javascript – 在删除之前注入JS代码

前端之家收集整理的这篇文章主要介绍了javascript – 在删除之前注入JS代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

是否保证此代码

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的异步代码,则会推迟.

猜你在找的HTML相关文章