2

This is in reference to SharePoint 2010.

I am writing some custom javascript that needs to know when the page has finished loading. To accomplish this I am pushing my callback function on to the _spBodyOnLoadFunctionNames array. This works great most of the time, but will sporadically fail and my handler will never get called.

Tracing it back, it looks like this array is never getting flushed because of issues with loading core.js.

In _spBodyOnLoadWrapper in init.js there is the call below which initiates the array being flushed. The ProcessDefaultOnLoad function is never getting called, and core.js does not show up as being loaded in the browser. The page appears to function just fine, but none of the functions in _spBodyOnLoadFunctionNames get called.

ExecuteOrDelayUntilScriptLoaded(ProcessDefaultOnLoad, "core.js");

Is _spBodyOnLoadFunctionNames the proper way to determine when a page has finished loading? Is there anything that would cause core.js not to be loaded?

A few more data points:

  1. I have only seen the issue in Chrome. With it being sporadic I can't say for sure that it never happens in other browsers.

  2. It appers to be happening more often after clearing the cache. Perhaps its a timing issue?

4 Answers 4

1

Where have you placed the content editor webpart???, Try palcing the CEW at the bottom of the page. I mean edit web part--> Layout--> Zone Index Change this to a different number so that the CEW appears on the bottom of the page

1

Have you tried using it together with the "_spBodyOnLoadCalled" variable?

if (!window._spBodyOnLoadCalled) {
    window._spBodyOnLoadFunctionNames.push("ProcessDefaultOnLoad");
} else {
    ProcessDefaultOnLoad();
}

It worked for me with this extra condition. I guess because the "_spBodyOnLoadFunctionNames" is a simple string array, nothing happens if you add your custom function's name in it after the _spBodyOnLoad event was handled.

1
  • Uncaught TypeError: Cannot read property 'push' of undefined
    – vapcguy
    Commented Aug 25, 2021 at 23:45
0

Apparently this is a known issue with SP 2010 and Chrome.

http://withinsharepoint.com/archives/256 http://mosswell.blogspot.com/2013/06/sharepoint-2010-and-chrome-javascript.html

1
  • First link doesn't work anymore. Best to describe what is on blogs so that answers aren't entirely dependent on them. Jist of 2nd link: Sys.Application.add_init(chromeNudgeDelay); and in that delay function that's called, put in if (navigator && navigator.userAgent && /chrome/.test(navigator.userAgent.toLowerCase())) { setTimeout(chromeNudge, 250); - but this didn't help me, either. It says 'Sys is not defined'.
    – vapcguy
    Commented Aug 25, 2021 at 23:40
0

This is very old question but I faced same issue recently. I was loading my content editor webpart in a publishing page. When I publish my page I got same issue. If I load same content in site pages then it works great. SP.SOD.executeFunc is the solution in my case.

I used following code to solve the issue.

$(document).ready(function(){
  SP.SOD.executeFunc('SP.js', 'SP.ClientContext',queryFunction);
});
1
  • I get "SP is not defined" doing this.
    – vapcguy
    Commented Aug 25, 2021 at 23:30

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.