The CEWP is loading a HTML page stored in the Site Assets library. The HTML contains a few inputs and some Javascript.

Javascript is:

 Function myInit(){
    Ctx = new SP.ClientContext();
 ExecuteOrDelayUntilScriptLoaded(myInit, "sp.js")

Normally this code runs fine if you navigate to the page but not if you open the browser from a link directly to the page.

If the browser opens directly at the page the HTML controls load & so does sp.js but the myInit() never gets fired.



I found even when using

 Function myInit ( ) { 
      console.log ( " here1 " ); 
       If (SP){
               console.log ( " here2 " );
       } else {
                setTimeout (myInit, 400);

It will only output: here1 once

  • Any reason you're doing this in a CEWP on page rather than running it in in the page head? Sep 1, 2015 at 19:00
  • Because it is small tool inside a file stored in SiteAssets to be used on a single webpart page as a dialog.
    – user13186
    Sep 2, 2015 at 20:01

1 Answer 1


sp.js is probably not loaded on your site and the SP.SOD.executeOrDelayUntilScriptLoaded function does not force the load of a library. To load a library use the following: SP.SOD.executeFunc('sp.js', 'SP.ClientContext', callbackFunction);

The diffrence as described in the MSDN:


Executes the specified function if the file containing it is loaded; otherwise, adds it to the pending job queue.


Ensures that the specified file that contains the specified function is loaded and then runs the specified callback function.

You need both to ensure that your script is triggered on every page. Here's an example:

function myInit() {
    console.log('myInit triggered');
    var ctx = new SP.ClientContext();
SP.SOD.executeOrDelayUntilScriptLoaded(myInit, 'sp.js'); //this triggers myInit when sp.js is aviable
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { console.log('sp.js was loaded by executeFunc'); }); //this triggers the function if executeFunc loaded sp.js and initialized the object SP.ClientContext - this does not necessarily trigger if sp.js is already on the page

Result on a page without loaded sp.js (an OOTB settings.aspx for example): result w.o. sp.js

Result on a page with loaded sp.js:

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.