Is it guaranteed that this code
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');");
will wait for the code passed to the runEmbeddedJSInPageEnvironment to finish first, and only then remove it from the page by calling removeChild function?
Or can it be removed before this code finished to execute?
In interpreted languages, the code is run from top to bottom and the result of running the code is immediately returned. You don't have to transform the code into a different form before the browser runs it.
The function setTimeout is called with 2 arguments: a message to add to the queue, and a time value (optional; defaults to 0 ). The time value represents the (minimum) delay after which the message will be pushed into the queue.
It separates HTML and code. It makes HTML and JavaScript easier to read and maintain.
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
scriptelement that is not marked as being "parser-inserted" experiences one of the events listed in the following list, the user agent must synchronously prepare thescriptelement:
- The
scriptelement gets inserted into a document, at the time the node is inserted according to the DOM, after any otherscriptelements inserted at the same time that are earlier in theDocumentin 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With