In this tutorial it is present the mode to get and modify content in an IFRAME, from the main page, using JavaScript.
It is important to know that the page that is included in IFrame must be on the same server as the main page which contains it, or else it will not work for security reasons.

Going straight to the point, to get and modify the content of an IFrame, you can use the contentWindow property, it returns the "window object" of a specified frame or iframe.
Here's an example to understand how to apply this property.

1) First create a file "ifr.htm" that will contain the HTML code of the page that will be displayed in the IFrame.

Code ifr.htm

<head><title>Iframe page</title></head>
 Content to be displayed in iframe ...

2) Define the main page, for example "main.html" (in the same folder on the server), in which write the code to include the IFrame and the JavaScript that will retrieve and modify the content of the IFrame.
- Necessary explanations are in the script code.

Code main.html

<head><title>Main page</title></head>
<script type="text/javascript">
function get_iframe(ifr_id) {
  // gets the object that refers to the iframe, uasing its id
    var myIFrame = document.getElementById(ifr_id); // Apply the property "contentWindow" to myIFrame object // In this way we get the iframe content var content = myIFrame.contentWindow.document.body.innerHTML; alert("Content: \n" + content); // Displays an Alert with the data from iframe // Define a new text that will replace the content of the iframe content = '<font color="blue">Text added with Javascript, from the main page</font>'; // Modify the iframe content myIFrame.contentWindow.document.body.innerHTML = content; } </script> <h3>Main page</h3> <a href="#" onclick="get_iframe('ifr')">Retrieve /Modify IFrame</a><br><br> <iframe src="ifr.htm" id="ifr" name="ifr"></iframe> </body> </html>

- This example will display the following result:

The main page

Retrieve /Modify IFrame

Content to be displayed in iframe ...
- When you click the link "Retrieve /Modify IFrame", the function "get_iframe()" will be called, that gets the content from the Iframe, and displays it in an Alert window, then sets a text to replace the iframe content. If you click again, it will retrieve and display the new content.

Another way to access the content of an iframe is to use the parent property and the IFrame's name. With this method you can access JavaScript variables and functions defined in IFrame.
This method is presented in the next tutorial, in which you will see how to get and modify the content of an IFrame by accessing a JS script that is created in another IFrame.

Daily Test with Code Example

Which tag is used to include external CSS file in web page?
<body> <script> <link>
<link href="/templ/style.css" rel="stylesheet" type="text/css" />
Which CSS property sets the text size?
font-weight text-decoration font-size
h2 {
  font-size: 1em;
Indicate the JavaScript property that can add HTML code into an element.
text value innerHTML
document.getElementById("someID").innerHTML = "HTML content";
Click on the function that returns the number of characters of a string in PHP.
count() strlen() stristr()
$str = "";
$nr_chr = strlen($str);
echo $nr_chr;       // 22
Get and Modify content of an Iframe

Last accessed pages

  1. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (9426)
  2. Get the Timestamp of First and Last day of Month (170)
  3. Get the value of the selected /checked checkboxes in a form (6252)
  4. PHP Chat Script (9768)
  5. Check the file type before Upload (1518)

Top accessed pages

  1. PHP-MySQL free course, online tutorials PHP MySQL code (11381)
  2. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (10996)
  3. Get Attribute (ID, Class, Name, Title, Src) with jQuery (10625)
  4. PHP Chat Script (9768)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (9426)