Will injected JS code finish before removing

后端 未结 1 975
星月不相逢
星月不相逢 2021-01-13 07:43

Is it guaranteed that this code

function runEmbeddedJSInPageEnvironment(code) {
  var e = document.createElement(\'script\');
  e.type = \'text/javascript\';         


        
相关标签:
1条回答
  • 2021-01-13 07:48

    Yes, according to HTML5 the code will run before removing the script element.

    When you insert it into the document, it's immediately prepared:

    When a script element that is not marked as being "parser-inserted" experiences one of the events listed in the following list, the user agent must synchronously prepare the script element:

    • The script element gets inserted into a document, at the time the node is inserted according to the DOM, after any other script elements inserted at the same time that are earlier in the Document in tree order.

    At step 15 of the prepare a script algorithm, since the script doesn't have a src attribute and has not been flagged as "parser inserted", your case would be the last one:

    Otherwise: The user agent must immediately execute the script block, even if other scripts are already executing.

    But of course, if that script has asynchronous code like setTimeout, that will be postponed.

    0 讨论(0)
提交回复
热议问题