title: Execute JavaScript function after SharePoint is initialized
body: <p>I have a custom JS script which I load into SharePoint and have problems to get my init method executed after SP is finished with its own initializing. </p>
  
  <p><strong>_spBodyOnLoadFunctionNames</strong></p>
  
  <p>I tried the "official" way first and added my function name to the list of executed functions after body load with <code>_spBodyOnLoadFunctionNames.push("myInitMethod");</code> but that does not fire on every page load, I can't rely on that.</p>
  
  <p><strong>ExecuteOrDelayUntilScriptLoaded</strong></p>
  
  <p>Then I tried to use <code>ExecuteOrDelayUntilScriptLoaded(myInitMethod, "sp.js");</code> function but it does not fire on every page load either.</p>
  
  <p>Both ways work - but not every time. I assume that my script is loaded sometimes <strong>before</strong> the SP is initialized. This happens mostly on Chrome but on IE as well.</p>
  
  <p>How can I make sure that my script is executed when SP is ready?</p>
  
  <hr>
  
  <p><strong>Note</strong>: There is an interesting behaviour when the page is loaded and the SP object is not fully initialized (the registered functions in ExecuteOrDelayUntilScriptLoaded has not been called): As soon as I click on the "Navigate Up" anchor in the page (where you can see the hiarchy of the subsites) the following files gets loaded and my init function (registered in ExecuteOrDelayUntilScriptLoaded) gets called!</p>
  
  <ul>
  <li>core.debug.js</li>
  <li>sp.core.debug.js</li>
  <li>ScriptResx.ashx</li>
  <li>sp.ui.dialog.debug.js</li>
  <li>sp.runtime.debug.js</li>
  <li>sp.debug.js</li>
  </ul>
  
  <p>So everything is fine after that click - but why not on pageload as it should be?</p>