使用jQuery / javascript如何检查是否已在页面中调用JS文件(SP.JS)? [英] Using jQuery / javascript How to check if JS file ( SP.JS) is already called in a page?

查看:161
本文介绍了使用jQuery / javascript如何检查是否已在页面中调用JS文件(SP.JS)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一下是否已经在document.ready中加载了一个特定的JS文件。

I want to check if a particular JS file is already loaded in document.ready.

这样的事情:

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

JS文件不是任何插件。

The JS File is not any plugin.

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

Its basically a JS file related to SharePoint like Sp.JS.

我们只知道文件名。

[更新 - 添加代码]

[Update - Added the code ]

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

I have added the below code and it throws an error in console : SP.Runtime.js is already loaded.

如果我删除SP.Runtime.js的加载,我的代码在某些未加载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.

$(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文件的方法。

SharePoint JavaScript Library, in particular SP.SOD namespace contains methods for loading/ensuring JavaScript files.


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

  1. SP.SOD.executeOrDelayUntilScriptLoaded - executes the specified function if the file containing it is loaded, for example:

ExecuteOrDelayUntilScriptLoaded(myfunc, "SP.js");

function myfunc()
{
}

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

In that case myfunc will be invoked after sp.js file is loaded

SP.SOD.executeFunc - 确保加载
包含指定函数的指定文件,然后运行
指定的回调函数,例如:

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 demand scripts.

这篇关于使用jQuery / javascript如何检查是否已在页面中调用JS文件(SP.JS)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆