是否有可能停止动态插入的脚本标记? [英] Is it possible to stop a dynamically inserted script tag?

查看:83
本文介绍了是否有可能停止动态插入的脚本标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想这样的东西是这样的:

  var script_tag = document.createElement('script'); 
script_tag.setAttribute('type','text / javascript');
script_tag.setAttribute('src','http://fail.org/nonexistant.js');
document.getElementsByTagName('head')[0] .appendChild(script_tag);

//这样的东西!!!
script_tag.abort();


解决方案

无法以任何跨浏览器的方式进行。只要脚本标签被添加到头部,它就会被下载并解析,甚至删除节点也不会阻止它。



就是说,Firefox有< a href =https://developer.mozilla.org/en-US/docs/Web/API/Document/onbeforescriptexecute =nofollow noreferrer> document.onbeforescriptexecute ,这是可以取消的(默认操作是执行目标脚本)。这个事件最初是在标准轨道上,但由于缺少有效的用例,最近从HTML规范中删除。


Is it somehow possible to stop a script tag from loading after is has been added to the head of a HTML document?

I would like to have something like this:

var script_tag = document.createElement('script');
script_tag.setAttribute('type', 'text/javascript');
script_tag.setAttribute('src', 'http://fail.org/nonexistant.js');
document.getElementsByTagName('head')[0].appendChild(script_tag);

// something like this!!!
script_tag.abort();

解决方案

It's not possible in any cross-browser manner. As soon as the script tag is added to the head it will be downloaded and parsed and even removing the node won't stop it.

That being said, Firefox has document.onbeforescriptexecute, which is cancellable (where the default action would be to execute the target script). This event was originally on a standards track but was recently removed from the HTML spec due to a lack of valid use cases.

这篇关于是否有可能停止动态插入的脚本标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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