是否保证此代码
function runEmbeddedJSInPageEnvironment(code) {
var e = document.createElement('script');
e.type = 'text/javascript';
e.appendChild(document.createTextNode(code));
(document.head || document.documentElement).appendChild(e);
e.parentNode.removeChild(e);
}
runEmbeddedJSInPageEnvironment("$('#someform').off('submit');");
将等待传递给runEmbeddedJSInPageEnvironment的代码首先完成,然后通过调用removeChild函数将其从页面中删除?
或者可以在此代码执行完毕之前将其删除?
最佳答案
是的,根据HTML5,代码将在删除脚本元素之前运行.
当您将其插入文档时,它立即prepared:
When a 07001 element that is not marked as being
07002 experiences one of the events listed in the
following list,the user agent must synchronously 07003 the
07001 element:
- The 07001 element gets 07006,at the time the 07007 according to the DOM,after any
other 07001 elements inserted at the same time that are
earlier in the 07009 in 070010.
在prepare a script算法的第15步,由于脚本没有src
属性且未被标记为“parser inserted”,因此您的案例将是最后一个:
Otherwise: The user agent must immediately 070014,even if other scripts are already executing.
但是,当然,如果该脚本具有类似setTimeout的异步代码,则会推迟.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。