function injectHTML(){
//step 1: get the DOM object of the iframe.
var iframe = document.getElementById('test_iframe');
var html_string = '<html><head></head><body><p>Hi I am ali Hesari</p></body></html>';
/* if jQuery is available, you may use the get(0) function to obtain the DOM object like this:
var iframe = $('iframe#target_iframe_id').get(0);
*/
//step 2: obtain the document associated with the iframe tag
//most of the browser supports .document. Some supports (such as the NetScape series) .contentDocumet, while some (e.g. IE5/6) supports .contentWindow.document
//we try to read whatever that exists.
var iframedoc = iframe.document;
if (iframe.contentDocument)
iframedoc = iframe.contentDocument;
else if (iframe.contentWindow)
iframedoc = iframe.contentWindow.document;
if (iframedoc){
// Put the content in the iframe
iframedoc.open();
iframedoc.writeln(html_string);
iframedoc.close();
} else {
//just in case of browsers that don't support the above 3 properties.
//fortunately we don't come across such case so far.
alert('Cannot inject dynamic contents into iframe.');
}
}
$("button").click(function() {
injectHTML();
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.