是否保证此代码
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的异步代码,则会推迟.