4

I want to run some JavaScript (not jQuery) after the page loads in SharePoint.

What method should I include in my script?

1 Answer 1

6

Many things happen on page after $(document).ready(). SharePoint 2013 does provide a few options to execute JavaScript function after page loads.

  1. Script on Demand (load a .js file then execute you code):

    function stuffThatRequiresSP_JS(){
        //your code
    }
    SP.SOD.executeFunc("sp.js", stuffThatRequiresSP_JS) ;
    
  2. Delay until loaded (wait for a .js file to load, then run):

    function stuffToRunAfterSP_JS(){
        //your code
    }
    ExecuteOrDelayUntilScriptLoaded(stuffToRunAfterSP_JS, "sp.js");
    
  3. Run function after other stuff finishes loading:

    function runAfterEverythingElse(){
        // your code
    }
     _spBodyOnLoadFunctionNames.push("runAfterEverythingElse");
    

Sources:

  1. executeFunc: http://msdn.microsoft.com/en-us/library/ff409592(v=office.14).aspx

  2. ExecuteOrDelayUntilScriptLoaded: http://msdn.microsoft.com/en-us/library/ff411788(v=office.14).aspx

  3. For _spBodyOnLoadFunctionNames: source

7
  • The function $(document).ready(). is jQuery.. isn't it? What is the javascript equivalent? Commented Oct 31, 2018 at 13:05
  • Yes, the other 3 options given in answer are using javascrip and not jquery. Commented Oct 31, 2018 at 13:07
  • Yes.. just waiting 3 minutes so they will let me :-) Commented Oct 31, 2018 at 13:12
  • One last question Ganesh... What is the javascript equivalent of this jQuery function? var refiner = $getClientControl(document.getElementById("MSOZoneCell_WebPartWPQ4")); Commented Oct 31, 2018 at 13:57
  • Where are you using this $getClientControl function? And for what? Commented Oct 31, 2018 at 14:04

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.