Insert a Script into a iFrame's Header, without clearing out the body of the iFrame
Posted
by nobosh
on Stack Overflow
See other posts from Stack Overflow
or by nobosh
Published on 2010-04-17T02:18:03Z
Indexed on
2010/04/17
2:23 UTC
Read the original article
Hit count: 505
Hello,
I'm looking to add a script to an iFrame's header while not losing everything contained in the iFrame's body or header...
here is what I have right now which does update the iFrame with the new script, but it cleans everything in the iframe out, not appends which is what I'd like. thxs! B
// Find the iFrame
var iframe = document.getElementById('hi-world');
// create a string to use as a new document object
var val = '<scr' + 'ipt type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></scr' + 'ipt>';
// get a handle on the <iframe>d document (in a cross-browser way)
var doc = iframe.contentWindow || iframe.contentDocument;
if (doc.document) { doc = doc.document;}
// open, write content to, and close the document
doc.open();
doc.write(val);
doc.close();
© Stack Overflow or respective owner