【发布时间】:2016-08-02 14:57:35
【问题描述】:
是否可以停止加载以下方式加载的 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 调试器工具的源选项卡下。有可能的解决方案,例如使用//# sourceURL=dynamicScript.js,但我必须将此行放在所有 js 文件中。我不想编辑所有的 js 文件。如果更改本地化到 1 个地方,那很好。
【问题讨论】:
-
视情况而定,可以,可以;但是,如果条件与脚本的大小或内容有关,那么您将不得不使用 XMLHttpRequest 来加载您的脚本。如果是这样,那我可以为你提供一个很好的答案。
-
假设有2个链接,点击每个链接使用
makeScript()加载js文件。如果用户点击第一个链接并快速点击第二个链接,我想中止加载第一个链接的js文件。如果 js 文件的加载没有停止,那么脚本中可能存在 TypeErrors,因为当单击另一个链接时,第一个链接的变量被删除,加载脚本将使用该变量。由于找不到这些变量,所以会出现 TypeError。
标签: javascript jquery jqxhr