使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:04:19  来源:igfitidea点击:

Using jQuery / javascript How to check if JS file ( SP.JS) is already called in a page?

javascriptjquerysharepointdocument-ready

提问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 文件的方法。

  1. 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 myfuncwill be invoked aftersp.js file is loaded

  2. SP.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.

  1. SP.SOD.executeOrDelayUntilScriptLoaded- 如果加载了包含它的文件,则执行指定的函数,例如:

    ExecuteOrDelayUntilScriptLoaded(myfunc, "SP.js");
    
    function myfunc()
    {
    }
    

    在这种情况下myfunc将在加载 sp.js 文件调用

  2. 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.onloadand $(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).loador window.onloadinstead of $(document).ready, by example:

更新:如果您在 SharePoint 中的任何页面中使用您的代码,那么您不需要强制加载本机 JS 文件,您只需要在页面加载过程的正确时刻执行您的代码。尝试使用$(window).loadwindow.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... }); });