Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何中止脚本加载_Javascript_Jquery_Jqxhr - Fatal编程技术网

Javascript 如何中止脚本加载

Javascript 如何中止脚本加载,javascript,jquery,jqxhr,Javascript,Jquery,Jqxhr,是否可以停止加载以下方式加载的JS文件,是否有任何事件处理程序 function makeScript(url){ script = document.createElement("script"); script.src = url; //when this line is executed the script will be loaded by the browser //is there an event where the below scrip

是否可以停止加载以下方式加载的JS文件,是否有任何事件处理程序

function makeScript(url){    
    script = document.createElement("script");
    script.src = url;
    //when this line is executed the script will be loaded by the browser
    //is there an event where the below script loading can be stopped based                                                                   
    //on some condition                        
    document.head.appendChild(script); 
}

我不想通过ajax调用加载脚本,因为js文件不会出现在Chrome调试器工具的sources选项卡下。有一些可能的解决方案,比如使用
/#sourceURL=dynamicScript.js
,但我必须将这一行放在所有js文件中。我不想编辑所有的js文件。如果更改被本地化为1个位置,则很好。

根据注释,尝试以下操作:

function makeScript(url)
{
    script = document.createElement("script");

    script.onload = function()
    {
        if (!this.abort)
        {
            document.head.appendChild(script);
            return;
        }
    };

    script.src = url;

    return script;
}
var js1 = makeScript('path/to/file.js');

js.abort = true; // will not run
您可以这样使用它:

function makeScript(url)
{
    script = document.createElement("script");

    script.onload = function()
    {
        if (!this.abort)
        {
            document.head.appendChild(script);
            return;
        }
    };

    script.src = url;

    return script;
}
var js1 = makeScript('path/to/file.js');

js.abort = true; // will not run
因此,基本上,您所需要做的就是onclick->运行相应地触发上述JavaScript的函数;可以创建一个保存最后一次脚本加载的对象,并将“onclick”assign makeScript return元素指定给该对象,以便替换或删除它。

在您的注释之后


假设有2个链接,单击每个链接使用makeScript()加载js文件。如果用户单击第一个链接并快速单击第二个链接,我想中止加载第一个链接的js文件。如果未停止加载js文件,则脚本中可能存在类型错误,因为当单击另一个链接时,第一个链接的变量将被删除,加载脚本将使用该变量

这是我的想法

您可以维护一个全局变量,该变量将保存要加载的脚本。根据这个变量,您可以执行
document.head.appendChild(脚本)代码行

示例代码

var scriptFilesConfig; //make this global scope variable
单击每个链接,将此url分配到全局变量中

假设您单击了第一个链接,那么变量将是

scriptFilesConfig = "firstscript.js";
好的,现在我立即单击了第二个链接,然后您必须将此url分配给变量。所以现在你的物体会是这样的

scriptFilesConfig = "secondScript.js";
现在,您将在任何给定点执行
document.head.appendChild(脚本)仅当脚本的url与全局变量匹配时

所以你的代码应该是

function makeScript(url){    
    script = document.createElement("script");
    script.src = url;
    if( url === scriptFilesConfig){  // append it only if the urls match.   
     document.head.appendChild(script); 
    }
}    

视情况而定,可以;但是,如果条件与脚本的大小或内容有关,则必须使用XMLHttpRequest加载脚本。如果是这种情况,那么我可以为您提供一个很好的答案。假设有2个链接,单击每个链接使用
makeScript()
)加载js文件。如果用户单击第一个链接并快速单击第二个链接,我想中止加载第一个链接的js文件。如果未停止加载js文件,则脚本中可能存在类型错误,因为当单击另一个链接时,第一个链接的变量将被删除,加载脚本将使用该变量。由于找不到这些变量,所以会出现TypeError。有时会出现
document.head.appendChild(脚本)将被执行,但在它完成之前,另一个链接将被单击->第一个链接的变量将被删除,最后当脚本准备好执行时..将出现脚本错误。应该发生的是,当点击第二个链接时,加载脚本的请求应该被取消。可能使用到目前为止提供的答案的组合。请记住向上投票有用的和相关的答案,因为如果其他人看到小费是这样显示的,这也会激励他们回答。@Mumzee一旦你执行
document.head.appendChild(script)您可以禁用所有其他链接,对吗?
脚本。将不执行onload
。必须加载脚本,然后才能启动onload。