Lime
Lime

Reputation: 13534

Javascript's document.write prevents following code execution

I understand document.write prevents the executing of following javascript.

For example the document.write will prevent the execution of the following javascript until the document.write block has completely executed.

<!doctype html>
<script>
document.write('<script>alert(1)<'+'/script>');
alert('2');  
</script>

http://jsbin.com/alefob

You can take this even further and require for javascript to be loaded remotely before executing the following script.

<!doctype html>
<script>
document.write('<script src="remote_script.js"><'+'/script>');
alert(remote_script_variable);  
</script>

Obviously I could do a callback, but that's not what I'm looking for.

Question

Basically do any other functions have this unique functionality of preventing scripts from loading until completion (others functions that I might want to avoid)?

Assuming that no other functions have this unique functionality. Can document.write ever be used after page load without overwriting the pages content?

Already Understood

I know you can do the following along with using yennop.js

var script = document.createElement('script');
var currentScript = document.getElementsByTagName('script')[0];
currentScript.parentNode.insertBefore(script,currentScript);
script.src = '//some url';

Upvotes: 1

Views: 294

Answers (1)

Mike Samuel
Mike Samuel

Reputation: 120486

Synchronous XHR stops the world in many browsers until the response is received or the request errors out.

And obviously, document.writeln stops the world in the same way as document.write.

Upvotes: 1

Related Questions