Tuesday 3 September 2013

SP.SOD.executeOrDelayUntilScriptLoaded not working reliably in Chrome

SharePoint 2010 ExecuteOrDelayUntilScriptLoaded is not working reliably in Chrome.  It does work reliably in IE, however..
Even a simple example like:

SP.SOD.executeOrDelayUntilScriptLoaded(blah, "SP.js");
function blah(){
 alert('!');
}

does not work reliably when the page is loaded.  It will *sometimes* work with a complete page refresh. But this is unacceptable when using SP javascript API for web-facing applications..
I discovered something that seems to have fixed the issue for me.
My master page has the following

<body scroll="no" onload="if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();" class="v4master">

I found that if I used the in-browser script console and called _spBodyOnLoadWrapper(), the scrollbars would be added properly to my window and the SOD functions would work properly again.
It seems to me that the body onload is not being called reliably in Chrome. I'm not sure what could be causing this, as the onload event seems to be fired reliably on a very simple html page when trying to reproduce the issue.

Since I am using jQuery anyway, I pulled the code from the body onload and instead used a jQuery document ready method to call this: 

<script type="text/javascript">
  $(document).ready(function(){
   if (typeof(_spBodyOnLoadWrapper) != 'undefined') 
   {
    _spBodyOnLoadWrapper();
   }
  });
</script>

and now my body tag looks like this:

<body scroll="no" class="v4master">

Anyway - this seems to have solved my issue. Please try and let me know if this works for you as well.

Share This!


1 comment:

Translate

Total Pageviews

Powered By Blogger · Designed By Seo Blogger Templates