最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Execute JavaScript function after SharePoint is initialized - Stack Overflow

programmeradmin3浏览0评论

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.

_spBodyOnLoadFunctionNames

I tried the "official" way first and added my function name to the list of executed functions after body load with _spBodyOnLoadFunctionNames.push("myInitMethod"); but that does not fire on every page load, I can't rely on that.

ExecuteOrDelayUntilScriptLoaded

Then I tried to use ExecuteOrDelayUntilScriptLoaded(myInitMethod, "sp.js"); function but it does not fire on every page load either.

Both ways work - but not every time. I assume that my script is loaded sometimes before the SP is initialized. This happens mostly on Chrome but on IE as well.

How can I make sure that my script is executed when SP is ready?


Note: 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!

  • core.debug.js
  • sp.core.debug.js
  • ScriptResx.ashx
  • sp.ui.dialog.debug.js
  • sp.runtime.debug.js
  • sp.debug.js

So everything is fine after that click - but why not on pageload as it should be?

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.

_spBodyOnLoadFunctionNames

I tried the "official" way first and added my function name to the list of executed functions after body load with _spBodyOnLoadFunctionNames.push("myInitMethod"); but that does not fire on every page load, I can't rely on that.

ExecuteOrDelayUntilScriptLoaded

Then I tried to use ExecuteOrDelayUntilScriptLoaded(myInitMethod, "sp.js"); function but it does not fire on every page load either.

Both ways work - but not every time. I assume that my script is loaded sometimes before the SP is initialized. This happens mostly on Chrome but on IE as well.

How can I make sure that my script is executed when SP is ready?


Note: 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!

  • core.debug.js
  • sp.core.debug.js
  • ScriptResx.ashx
  • sp.ui.dialog.debug.js
  • sp.runtime.debug.js
  • sp.debug.js

So everything is fine after that click - but why not on pageload as it should be?

Share Improve this question edited Feb 2, 2015 at 2:57 Alex Angas 60k41 gold badges143 silver badges212 bronze badges asked Jul 5, 2012 at 7:16 MarcMarc 6,7719 gold badges50 silver badges79 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

It seems that this behaviour is related to some issues between SP 2010 and Google Chrome - I don't have this issues on other browsers.

This is a timing issue that somehow occurs only on non-IE browsers. See http://withinsharepoint./archives/256 for an explanation and very easy fix.

Hey I came across your question when I was looking for a way to delay my JavaScript from loading until sp.js has.

The reason your code you provided works some of the time is because (some of the time) SharePoint doesn't initialize all of it's codebase. This is a big problem in Google Chrome, but can happen in other browsers (non-IE) as well. To work around that particular issue you can do something like this:

if (typeof(_spBodyOnLoadWrapper)!='undefined'){_spBodyOnLoadWrapper();}

I put mine inside of $(document).ready().

And thanks for answering my question :)

发布评论

评论列表(0)

  1. 暂无评论