Sharepoint - ExecuteOrDelayUntilScriptLoaded not executing after page publish

Here is my fix to the problem.

It seems in SharePoint 2013 calling ExecuteOrDelayUntilScriptLoaded() doesn't work on published pages.

This is the correct way to do it according to Microsoft (see: http://msdn.microsoft.com/en-us/library/jj245759.aspx)

// Make sure the SharePoint script file 'sp.js' is loaded before your code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', startIt);

As a part of the new Minimal Download Strategy (MDS), sp.js is not loaded by default on published pages.

You will have to load your sp.js explicitly from the /_layouts/15 directory. Example (not tested):

$(document).ready(function(){

   var fileUrl = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/sp.js";

   $.getScript(fileUrl,function(){

        alert('Started');

    });

});

This change from SharePoint 2010 makes sense in a way that you should only load the sp.js file if you want to make use of the JavaScript Client Object Model. If you are going to use the REST API, you will not need the sp.js file.

Tags:

Javascript

Web