4

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

What method should I include in my script?

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
Martin Muldoon
  • 930
  • 12
  • 27

1 Answers1

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

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61