18

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?

5

5 Answers 5

26

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);
1
  • In my SP2013, I applied your method on MySite pages. But the function never fire. Do you know why?
    – Mark L
    Feb 6, 2015 at 3:11
4

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.

3
  • 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, 2013 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. Feb 27, 2013 at 5:27
  • Vardhaman, thanks for your help and will look more into the MDS features.
    – NYTom
    Feb 27, 2013 at 14:06
1

We had a similar issue when trying to build a custom webpart using JS; the code would not execute when Minimal Download Strategy (MDS) was turned on.

As it turns out, SharePoint seems to be smart enough to look for <script> tags with type="text/javascript", and disable MDS when required on a specific page.

So we added a .html file to SiteAssets, let's say /sites/mysite/SiteAssets/test.html. And added some content to it:

<script type="text/javascript">
    console.log("test");
</script>

Then we added a Content Editor Web Part to the page, with a link to the file. When clicking on "Home," the page is at first loaded with MDS, but then redirects to the actual .aspx when it discovers the <script>.

So, no need to deactivate MDS when specifying type="text/javascript". Without this, the code loads through MDS and does not execute. Lesson learned.

1
  • Wow, it was just that easy! I had my Script Editor code nested in <script></script> but nothing about the type="text/javacript". After messing around with all the Execute* functions without any success, I'm glad it was an easy solution, thank you!
    – targnation
    Feb 9, 2017 at 16:09
0

Deactivate MSD - Minimum Download Strategy.

Replace the ExecuteOrDelayUntilScriptLoaded(startIt, "sp.js"); with below

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', startIt);

This rectifies your problem.

-1

Try to use it as given below:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){alert('Test')});
1
  • please add more information to your answer . Feb 27, 2019 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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