View RSS Feed


Document.write issues

Rate this Entry
The Ajax Includes Script given at uses document.write for including external content. Advantage: you don't have to explicitly bring the js belonging to the external file(s) to the main page. But beware the results in certain cases! In IE, (external) scripts added to the page using document.write are not run until after the document.write that adds them has finished. Non-IE browsers will run them straight away. So
<script type="text/javascript">
document.write('<script src="bla.js"><\/script>');
</script >
where bla.js contains document.write('drive.'), produces in non-IE, but in IE. So in IE, document.write may put external javascript at the wrong place.
That is indeed what happens if we use the script given at to include the menu-files of Try for yourself. Things won't work in IE. (The only way to get out of this situation is to replace the external js belonging to the files_to_be_included with internal js).
Of course, the cases mentioned here are only illustrations of a more general problem that we may encounter if we use document.write for including external content in an 'unwise' way.
Arie Molendijk.

Submit "Document.write issues" to Submit "Document.write issues" to StumbleUpon Submit "Document.write issues" to Google Submit "Document.write issues" to Digg

Updated 05-16-2009 at 11:55 PM by molendijk (Forgot to mention category)

Tags: None Add / Edit Tags
JavaScript & Ajax