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?

Eric Alexander
  • 43,293
  • 10
  • 53
  • 93
NYTom
  • 781
  • 1
  • 9
  • 22
  • 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 http://social.msdn.microsoft.com/Forums/en-US/sharepointdevpreview/thread/65e08466-5608-4c74-baa6-c69e3df767ed/ 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
  • This is a possible duplicate of http://sharepoint.stackexchange.com/questions/58503/sp-sod-how-to-use-correctly/58636#58636 Execute Or Delay Until Script Loaded is for loaded a script, if its already loaded it won't fire, so you use it in conjunction with execute func – Hugh Wood Aug 01 '14 at 13:41

5 Answers5

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);
jcp
  • 1,464
  • 1
  • 21
  • 36
NYTom
  • 781
  • 1
  • 9
  • 22
  • In my SP2013, I applied your method on MySite pages. But the function never fire. Do you know why? – Mark L Feb 06 '15 at 03: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.

Vardhaman Deshpande
  • 10,462
  • 1
  • 26
  • 52
  • I looked into SP 2013 MDS and I am using a publishing site. So according to http://www.wictorwilen.se/sharepoint-2013---introduction-to-the-minimal-download-strategy-mds 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 05:27
  • Vardhaman, thanks for your help and will look more into the MDS features. – NYTom Feb 27 '13 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.

Erin L
  • 4,038
  • 1
  • 14
  • 33
Arg0n
  • 111
  • 3
  • Wow, it was just that easy! I had my Script Editor code nested in 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 09 '17 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.

Asad Refai
  • 5,971
  • 8
  • 34
  • 57
-1

Try to use it as given below:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){alert('Test')});
Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61