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

标签 javascript jquery sharepoint document-ready

我想检查某个特定的 JS 文件是否已加载到 document.ready 中。

类似这样的:

if(file already called/loaded) { // my code }
else {//some other code}

JS 文件不是任何插件。

它基本上是一个与 SharePoint 相关的 JS 文件,例如 Sp.JS。

我们只知道文件名。

[更新 - 添加代码]

我添加了以下代码,它在控制台中抛出错误:SP.Runtime.js 已加载。

如果我删除 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 //

                    });
            });
    });
    });
});

请提出建议。

谢谢

最佳答案

SharePoint JavaScript 库,特别是 SP.SOD命名空间包含加载/确保 JavaScript 文件的方法。

  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...
     });
    

    该行为与前面的示例类似,但主要区别 该函数还支持按需加载脚本。

关于javascript - 使用 jQuery/javascript 如何检查 JS 文件 ( SP.JS) 是否已在页面中调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29793475/

相关文章:

jquery 自动完成查询

javascript - 通过 Rest 访问 Sharepoint 列表的 JQuery Json

共享点列表版本历史导出到 excel

javascript - Next.js:如何在 Next.js 中使用 source-map-explorer

javascript - 旋转 css 3d 对象不能正常工作

Javascript 正则表达式仅返回一系列项目

c# - Sharepoint OLE DB - 无法插入记录? "Field not updateable"错误

php - Json 编码为非 ascii 字符?

jquery - 复选框选中后,所有其他复选框均取消选中并且不可选中

javascript - jQuery 立即从粘贴中检测文本字段中的值,而不是稍后