SharePoint 初始化后执行 JavaScript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11339488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:50:35  来源:igfitidea点击:

Execute JavaScript function after SharePoint is initialized

javascriptsharepointsharepoint-2010

提问by Marc

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.

我有一个自定义的 JS 脚本,我将它加载到 SharePoint 中,但在 SP 完成自己的初始化后执行我的 init 方法时遇到问题。

_spBodyOnLoadFunctionNames

_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.

我首先尝试了“官方”方式,并在正文加载后将我的函数名称添加到执行的函数列表中,_spBodyOnLoadFunctionNames.push("myInitMethod");但这不会在每个页面加载时触发,我不能依赖它。

ExecuteOrDelayUntilScriptLoaded

ExecuteOrDelayUntilScriptLoaded

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

然后我尝试使用ExecuteOrDelayUntilScriptLoaded(myInitMethod, "sp.js");函数,但它也不会在每个页面加载时触发。

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

两种方式都有效——但不是每次都有效。我假设我的脚本有时会SP 初始化之前加载。这主要发生在 Chrome 上,但也发生在 IE 上。

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

如何确保在 SP 准备就绪时执行我的脚本?



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!

注意:当页面被加载并且 SP 对象没有完全初始化(在 ExecuteOrDelayUntilScriptLoaded 中注册的函数没有被调用)时,有一个有趣的行为:只要我点击页面中的“向上导航”锚点(你在哪里可以看到子站点的层次结构)加载以下文件并调用我的 init 函数(在 ExecuteOrDelayUntilScriptLoaded 中注册)!

  • core.debug.js
  • sp.core.debug.js
  • ScriptResx.ashx
  • sp.ui.dialog.debug.js
  • sp.runtime.debug.js
  • sp.debug.js
  • core.debug.js
  • sp.core.debug.js
  • 脚本Resx.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?

所以在点击之后一切都很好 - 但为什么不是在页面加载上呢?

采纳答案by Marc

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.

这种行为似乎与 SP 2010 和 Google Chrome 之间的一些问题有关 - 我在其他浏览器上没有这个问题。

回答by Louis

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

这是一个以某种方式仅在非 IE 浏览器上发生的计时问题。有关解释和非常简单的修复,请参阅http://withinsharepoint.com/archives/256

回答by Markus

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

嘿,当我正在寻找一种方法来延迟我的 JavaScript 加载直到 sp.js 加载时,我遇到了你的问题。

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:

您提供的代码有时有效的原因是(有时)SharePoint 没有初始化它的所有代码库。这是 Google Chrome 中的一个大问题,但也可能发生在其他浏览器(非 IE)中。要解决该特定问题,您可以执行以下操作:

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

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

我把我的放在 $(document).ready() 里面。

And thanks for answering my question :)

感谢您回答我的问题:)