转自:http://blog.point42.nl/?tag=/SharePoint+2013
SP.SOD.executeOrDelayUntilScriptLoaded(MyFunction, ‘sp.js’);
When sp.js is loaded, the SOD is notified and MyFunction is executed. In SharePoint 2010 this works in 99% of the times it is called because SharePoint depends on the sp.js file in most of its UI, so you assume the file is always loaded. I say most of the times because sometimes it can be a choice not to load it and just go with the basic JavaScript files (init.js).
SharePoint 2013
In SharePoint 2013 this has changed. Now every SharePoint JavaScript file is loaded only when it’s used, using the same SOD principle. Now most times sp.js is never loaded until it is called by SharePoint. Our call using ExecuteOrDelayUntilScriptLoaded isn’t working anymore since SOD doesn’t gets the notification that sp.js is loaded.
The solution
To overcome this issue there is a simple solution:
1
2
3
|
if
(!SP.SOD.executeOrDelayUntilScriptLoaded(MyFunction, ‘sp.js’)){
LoadSodByKey(‘sp.js’);
};
|
This will force the loading of sp.js still using the SOD principles. Also this check works in the SharePoint 2010 context, so you can upgrade your scripts now so they will continue to work when upgrading to SharePoint 2013.