Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

In SharePoint 2013 I have a page that is using the Script Editor web part.
In the script editor web part I have the following:

ExecuteOrDelayUntilScriptLoaded(startIt, "sp.js");

function startIt() {
alert('Started');
}

How come the alert is called when the page is unpublished, but once I publish the page then ExecuteOrDelayUntilScriptLoaded() is not firing. I'm using IE.

Is this a possible bug?

share|improve this question
    
Any masterpage customisations? Either way, see if sp.js is indeed coming down when the page is published. –  James Love Feb 26 '13 at 17:10
    
No masterpage customizations. This seems to be the exact same issue as social.msdn.microsoft.com/Forums/en-US/sharepointdevpreview/… but I dont see any resolution. –  NYTom Feb 26 '13 at 17:17
    
@NYTom I don't understand your comment, there's an answer in the link you posted. –  Christophe Mar 12 '13 at 21:55
add comment

2 Answers 2

Here is my fix to the problem.

It seems in SP 2013 calling ExecuteOrDelayUntilScriptLoaded() doesnt work on published pages.

In SP2013 this is the correct way to do it according to MS @ 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);
share|improve this answer
add comment

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.

share|improve this answer
    
I looked into SP 2013 MDS and I am using a publishing site. So according to wictorwilen.se/… the MDS feature should be turned off and I check my site settings feature page and it is turned off. So MDS shouldnt be a factor here. –  NYTom Feb 26 '13 at 19:24
    
What I meant was not regarding only the MDS feature. SharePoint 2013 has an overall Minimal Download Policy where it only downloads files which are needed. In 2010, the sp.js used to load on every page even if you were not using it. This caused some performance hit to say the very least. More over, in 2013 you have the REST API which does not require the sp.js file. So if you want to use that file, its the developer's responsibility to load it from his code. –  Vardhaman Deshpande Feb 27 '13 at 5:27
    
Vardhaman, thanks for your help and will look more into the MDS features. –  NYTom Feb 27 '13 at 14:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.