View RSS Feed

molendijk

Document.write issues

Rate this Entry
The Ajax Includes Script given at http://www.dynamicdrive.com/dynamici...axincludes.htm 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
Code:
<script type="text/javascript">
document.write('dynamic.');
document.write('<script src="bla.js"><\/script>');
document.write('com.');
</script >
where bla.js contains document.write('drive.'), produces dynamic.drive.com. in non-IE, but dynamic.com.drive. 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 http://www.dynamicdrive.com/dynamici...axincludes.htm to include the menu-files of http://www.dynamicdrive.com/dynamici...menu/index.htm. 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 del.icio.us 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
Categories
JavaScript & Ajax

Comments