Is it guaranteed that this code
function runEmbeddedJSInPageEnvironment(code) {
var e = document.createElement(\'script\');
e.type = \'text/javascript\';
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.