JavaScript write to IFrame

This page discusses - JavaScript write to IFrame

JavaScript write to IFrame


This section illustrates you how to write to IFrame using JavaScript.

The <iframe> tag defines an inline frame which allows to embed an HTML document inside another HTML document. In the given example, we have created a simple HTML page adding a div element and a function writeToIFrame() which is called when the page is loaded. The method createElement() of document object creates a html iframe element. The variable div grabs the id of the div element which is added to the iframe?s document element by using the method appendChild().

Following code allows to display the iframe's document on Firefox:

doc = iframe.contentDocument ;  

Following code allows to display the iframe's document on Internet Explorer:

else if(iframe.contentWindow) 
doc = iframe.contentWindow.document; 

Here is the code:

<h2>Write to IFrame using JavaScript</h2>
function writeToIFrame() { 
var iframe = document.createElement("iframe"); 
var div = document.getElementById("div"); 
var doc = null; 
doc = iframe.contentDocument; 
else if(iframe.contentWindow) 
doc = iframe.contentWindow.document;; 
doc.write("Hello World!"); 

<body onLoad="writeToIFrame();">
<div id="div" style="border: solid 1px; height: 250px; width: 400px;"></div> 

Output will be displayed as:

Download Source Code:


Share on Google+Share on Google+

JavaScript write to IFrame

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JavaScript write to IFrame  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Firas Moussa
April 23, 2013
Toooooo many annoying ads :(((

why bothering yourself with this tutorial, just replace it with ads, and you'll gain more money. damn, this page is very very annoying