简体   繁体   English

使用jQuery / javascript如何检查是否已在页面中调用JS文件(SP.JS)?

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

I want to check if a particular JS file is already loaded in document.ready. 我想检查document.ready中是否已加载特定的JS文件。

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 谢谢

SharePoint JavaScript Library, in particular SP.SOD namespace 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: SP.SOD.executeOrDelayUntilScriptLoaded - 如果加载了包含它的文件,则执行指定的函数,例如:

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

    In that case myfunc will be invoked after sp.js file is loaded 在这种情况下,将在加载sp.js文件调用myfunc

  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.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 demand scripts. 该行为与前面的示例类似,但该函数还支持按需加载脚本的主要区别。

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 page where 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. 更新:如果您在SharePoint中的任何页面中使用您的代码,那么您不需要强制加载本机JS文件,您只需要在页面加载过程的恰当时刻执行您的代码。 Try using $(window).load or window.onload instead of $(document).ready , by example: 尝试使用$(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... }); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM