Javascript iframe caching issue

Posted by Brian on Stack Overflow See other posts from Stack Overflow or by Brian
Published on 2010-03-17T20:51:23Z Indexed on 2010/03/17 21:31 UTC
Read the original article Hit count: 259

I hava an iframe which loads some javascript via javascript. In internet explorer only, this works if and only if IE has a cached copy of the javascript. This means that reloading the page (or otherwise triggering the script to run again, if it's stuck in a function or whatever) will cause this code to work, otherwise it will not. In particular, the document.write call fails to happen.

Main Page:

<iframe height = "200" width = "200" id = "happy">
</iframe>
<script type="text/javascript">
 var a = document.getElementById("happy");
 scripttxt = '<a href="#" id="joy">JOY</a><'+'script type="text/javascript" src="fail.js"></'+'script>';
 a.src = "about:blank"; 
 a.contentWindow.document.open();
 a.contentWindow.document.write("<h3>Preview:</h3>" + scripttxt + "");
 a.contentWindow.document.close();
</script>

fail.js:

document.write(document.getElementById("joy"));

I realize I could use conditional comments to have IE skip document.open() and document.close() in the script of Main Page, but having IE skip document.open() and document.close() feels a bit hacky (Edit)...and breaks other things in IE.

© Stack Overflow or respective owner

Related posts about internet-explorer

Related posts about JavaScript