SharePoint 2013 在整个页面加载后添加 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16936250/
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-27 06:31:32  来源:igfitidea点击:

SharePoint 2013 add javascript after whole page load

javascriptsharepoint-2013

提问by Kamil Biela

Disclaimer: I have no experience with SharePoint2013.

免责声明:我没有使用 SharePoint2013 的经验。

I have problem - I must include/fire some javascript functions after the whole page has been loaded. I tried listening to DOMDocumentReady and window.load events, but sharepoint render the rest of page after those events.

我有问题 - 我必须在整个页面加载后包含/触发一些 javascript 函数。我尝试侦听 DOMDocumentReady 和 window.load 事件,但在这些事件之后 sharepoint 呈现页面的其余部分。

My question is: what I should do to be able to run script after whole page with ajax is rendered. Also I noticed that page navigation is based on hash (#) part. Obviously I must detect that moment also.

我的问题是:我应该怎么做才能在使用 ajax 呈现整个页面后运行脚本。我还注意到页面导航基于哈希(#)部分。显然我也必须检测到那个时刻。

Any help or even link to right page in documentation would be great!

任何帮助甚至指向文档中正确页面的链接都会很棒!

回答by Will Fawcett

You are right, MANY things happen on page after $(document).ready(). 2013 does provide a few options.

你是对的,在 $(document).ready() 之后的页面上发生了很多事情。2013 确实提供了一些选择。

1) Script on Demand: (load a js file then execute my code.)

1)按需脚本:(加载一个js文件然后执行我的代码。)

function stuffThatRequiresSP_JS(){
    //your code
}

SP.SOD.executeFunc("sp.js")

SP.SOD.executeFunc("sp.js")

2) Delay until loaded (wait for a js file, then run)

2)延迟直到加载(等待一个js文件,然后运行)

function stuffToRunAfterSP_JS(){
    //your code
}
ExecuteOrDelayUntilScriptLoaded(stuffToRunAfterSP_JS, "sp.js")

3) load after other stuff finishes

3)在其他东西完成后加载

function runAfterEverythingElse(){
    // your code
}
_spBodyOnLoadFunctionNames.push("runAfterEverythingElse");

Sources:

资料来源:

executeFunc: http://msdn.microsoft.com/en-us/library/ff409592(v=office.14).aspx

executeFunc:http: //msdn.microsoft.com/en-us/library/ff409592(v=office.14) .aspx

ExecuteOrDelayUntilScriptLoaded: http://msdn.microsoft.com/en-us/library/ff411788(v=office.14).aspx

ExecuteOrDelayUntilScriptLoaded:http: //msdn.microsoft.com/en-us/library/ff411788(v=office.14) .aspx

Cant find a source on _spBodyOnLoadFunctionNames but I am using it in a few places.

无法在 _spBodyOnLoadFunctionNames 上找到来源,但我在几个地方使用它。

good luck.

祝你好运。

回答by Hyman A.

A documented event registration function that is roughly equivalent of _spBodyOnLoadFunctionNamesis SP.SOD.executeOrDelayUntilEventNotified. Call this to receive notification of the "sp.bodyloaded" event:

形成文件的事件注册功能大致相当于的_spBodyOnLoadFunctionNamesIS SP.SOD.executeOrDelayUntilEventNotified。调用它以接收“sp.bodyloaded”事件的通知:

SP.SOD.executeOrDelayUntilEventNotified(function () {
    // executed when SP load completes
}, "sp.bodyloaded");

This handler actually fires slightly before the _spBodyOnLoadFunctionNamesfunctions.

这个处理程序实际上在_spBodyOnLoadFunctionNames函数之前稍微触发。

This reference is for SharePoint 2010, but the SOD utility is present in SharePoint 2013: http://msdn.microsoft.com/en-us/library/office/ff410354(v=office.14).aspx

此参考适用于 SharePoint 2010,但 SOD 实用程序存在于 SharePoint 2013 中:http: //msdn.microsoft.com/en-us/library/office/ff410354(v=office.14).aspx

回答by José Quinto Zamora

There are different techniques used to provided our custom JavaScript code loaded before / in the middle / after OnLoad events in SharePoint:

有不同的技术用于提供我们在 SharePoint 中的 OnLoad 事件之前/中间/之后加载的自定义 JavaScript 代码:

ExecuteOrDelayUntilBodyLoaded.
Sys.Application.pageLoad.
document.ready Jquery.
_spBodyOnLoadFunctionNames.
_spBodyOnLoadFunction.
ExecuteOrDelayUntilScriptLoaded:sp.core.js.
SP.SOD.executeFunc: sp.js.
Sys.WebForms.PageRequestManager.PageLoaded

– ExecuteOrDelayUntilBodyLoaded function is always executed the first (but at this stage we can not access to SP methods). This could be usefull to execute our custom code at really earlier stage in the OnLoad process.

– ExecuteOrDelayUntilBodyLoaded 函数始终首先执行(但在此阶段我们无法访问 SP 方法)。这对于在 OnLoad 过程的早期阶段执行我们的自定义代码非常有用。

– There are two SharePoint onLoad functions _spBodyOnLoadFunctionNames and _spBodyOnLoadFunction. Always executed in the order. SO, if we want to execute some code after all functions included by us (or other devs) in _spBodyOnLoadFunctionNames, then is useful to use this one _spBodyOnLoadFunction, because is executed the last.

– 有两个 SharePoint onLoad 函数 _spBodyOnLoadFunctionNames 和 _spBodyOnLoadFunction。始终按顺序执行。所以,如果我们想在 _spBodyOnLoadFunctionNames 中我们(或其他开发者)包含的所有函数之后执行一些代码,那么使用这个 _spBodyOnLoadFunction 很有用,因为是最后执行的。

– ExecuteOrDelayUntilScriptLoaded:sp.core.js and SP.SOD.executeFunc: sp.js. are swapping the order of execution in a random way.

– ExecuteOrDelayUntilScriptLoaded:sp.core.js 和 SP.SOD.executeFunc: sp.js。正在以随机方式交换执行顺序。

– If we want to execute some functions after all functions (SP, after load functions, Yammer, etc.) we can use this function to attach the OnLoad event –> Sys.WebForms.PageRequestManager.PageLoaded.

– 如果我们想在所有函数之后执行一些函数(SP、after load 函数、Yammer 等)我们可以使用这个函数来附加 OnLoad 事件 –> Sys.WebForms.PageRequestManager.PageLoaded。

You can see the whole article explaining each type, pros and cons here: https://blog.josequinto.com/2015/06/16/custom-javascript-function-loaded-after-the-ui-has-loaded-in-sharepoint-2013/

您可以在此处查看解释每种类型、优缺点的整篇文章:https: //blog.josequinto.com/2015/06/16/custom-javascript-function-loaded-after-the-ui-has-loaded-in -sharepoint-2013/

Regards!

问候!

回答by WWC

https://mhusseini.wordpress.com/2012/05/18/handle-clicks-on-calendar-items-in-sharepoint-2010-with-javascript/

https://mhusseini.wordpress.com/2012/05/18/handle-clicks-on-calendar-items-in-sharepoint-2010-with-javascript/

The above link worked for me. He basically uses the ExecuteOrDelayUntilScriptLoaded to launch his calendar hook code after the SP.UI.ApplicationPages.Calendar.js loads.

上面的链接对我有用。他基本上使用 ExecuteOrDelayUntilScriptLoaded 在 SP.UI.ApplicationPages.Calendar.js 加载后启动他的日历挂钩代码。

Then, in the calendar hook he attaches his own function to the: SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed function.

然后,在日历挂钩中,他将自己的函数附加到: SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed 函数。

 _spBodyOnLoadFunctionNames.push("LaunchColorCodeCalendarScriptOnReady");


function LaunchColorCodeCalendarScriptOnReady() {


    ExecuteOrDelayUntilScriptLoaded(
        MyCalendarHook,
        "SP.UI.ApplicationPages.Calendar.js");


}

function MyCalendarHook() {
    var _patchCalendar = function () {
        //Do something to the items in the calendar here
        //ColorCodeCalendar();
    };

    var _onItemsSucceed = SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed;
    SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed = function ($p0, $p1) {
        _onItemsSucceed.call(this, $p0, $p1);
        _patchCalendar();
    };
}

回答by Ruslan Korkin

Here is nice article how to use the built-in SharePoint SOD (Script On Demand) functionality: http://www.ilovesharepoint.com/search/label/SOD

这是如何使用内置 SharePoint SOD(按需脚本)功能的好文章:http: //www.ilovesharepoint.com/search/label/SOD

It works ok both for SP 2010 and 2013.

它适用于 SP 2010 和 2013。

The idea of on demand script loading make really sense. SharePoint 2010 loads really a lot of JavaScripts - this takes time! So the idea is: first load the HTML and let it render by the browser so that the user is able to read the requested information as fast as possible. And in the second step load the behavior (the JavaScripts).

按需加载脚本的想法非常有意义。SharePoint 2010 加载了大量 JavaScript - 这需要时间!所以想法是:首先加载 HTML 并让它由浏览器呈现,以便用户能够尽快读取请求的信息。在第二步加载行为(JavaScripts)。