使用 jQuery / javascript 如何检查页面中是否已经调用了 JS 文件(SP.JS)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29793475/
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
Using jQuery / javascript How to check if JS file ( SP.JS) is already called in a page?
提问by user2598808
I want to check if a particular JS file is already loaded in document.ready.
我想检查一个特定的 JS 文件是否已经加载到 document.ready 中。
Something like this:
像这样的东西:
if(file already called/loaded) { // my code }
else {//some other code}
The JS File is not any plugin.
JS 文件不是任何插件。
Its basically a JS file related to SharePoint like Sp.JS.
它基本上是一个与 SharePoint 相关的 JS 文件,如 Sp.JS。
We just know the file name.
我们只知道文件名。
[Update - Added the code ]
[更新 - 添加了代码]
I have added the below code and it throws an error in console : SP.Runtime.js is already loaded.
我添加了以下代码,它在控制台中引发错误:SP.Runtime.js 已加载。
If I remove the loading of SP.Runtime.js my code doesnt work in some pages where Runtime.Js is not loaded by default.
如果我删除 SP.Runtime.js 的加载,我的代码在某些默认未加载 Runtime.Js 的页面中不起作用。
$(document).ready(function() {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "init.js",
function() {
$.getScript(scriptbase + "SP.Runtime.js",
function() {
$.getScript(scriptbase + "SP.js",
function() {
$.getScript(scriptbase + "SP.Taxonomy.js",
function() {
context = SP.ClientContext.get_current();
// My custom function //
});
});
});
});
});
Please suggest.
请建议。
Thanks
谢谢
回答by Vadim Gremyachev
SharePoint JavaScript Library, in particular SP.SODnamespace contains methods for loading/ensuring JavaScript files.
SharePoint JavaScript 库,特别是SP.SOD命名空间包含加载/确保 JavaScript 文件的方法。
SP.SOD.executeOrDelayUntilScriptLoaded- executes the specified function if the file containing it is loaded, for example:
ExecuteOrDelayUntilScriptLoaded(myfunc, "SP.js"); function myfunc() { }
In that case
myfunc
will be invoked aftersp.js file is loadedSP.SOD.executeFunc- ensures that the specified file that contains the specified function is loaded and then runs the specified callback function, for example:
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function (){ //your code goes here... });
The behavior is similar to previous example but the main difference that this function also supports load on demandscripts.
SP.SOD.executeOrDelayUntilScriptLoaded- 如果加载了包含它的文件,则执行指定的函数,例如:
ExecuteOrDelayUntilScriptLoaded(myfunc, "SP.js"); function myfunc() { }
在这种情况下
myfunc
将在加载 sp.js 文件后调用SP.SOD.executeFunc- 确保包含指定函数的指定文件被加载,然后运行指定的回调函数,例如:
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function (){ //your code goes here... });
该行为与前面的示例类似,但主要区别在于该函数还支持按需加载脚本。
回答by Isaac E. Krauss
If you only need to ensure that specific native JS files of SharePoint are loaded before executing your code then Vadim's answer is all what you need, however if you require to ensure the loading of all page elements including all JS files then you should use window.onload
.
如果您只需要确保在执行代码之前加载 SharePoint 的特定本机 JS 文件,那么 Vadim 的答案就是您所需要的,但是如果您需要确保加载所有页面元素,包括所有 JS 文件,那么您应该使用window.onload
.
Please take a look at this pagewhere people discuss about the differences between windows.onload
and $(document).ready()
.
请查看此页面,人们在此讨论windows.onload
和之间的区别$(document).ready()
。
window.onload vs $(document).ready()
window.onload vs $(document).ready()
UPDATE:
In case you are using your code in any page within SharePoint then you don't need to force the loading of native JS files, you only need to execute your code at the right moment of the page load process.
Try using $(window).load
or window.onload
instead of $(document).ready
, by example:
更新:如果您在 SharePoint 中的任何页面中使用您的代码,那么您不需要强制加载本机 JS 文件,您只需要在页面加载过程的正确时刻执行您的代码。尝试使用$(window).load
或window.onload
代替$(document).ready
,例如:
$(window).load(function(){
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function (){
context = SP.ClientContext.get_current();
//your code goes here...
});
});
$(window).load(function(){
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function (){
context = SP.ClientContext.get_current();
//your code goes here...
});
});