• Another useful tutorial about JavaScript and iframe on the page:
coursesweb.net/javascript/frame-iframe-object


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 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

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

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

<html>
<head><title>Main page</title></head>
<body>
<script>
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 top.iframeName instruction. With this method you can access JavaScript variables and functions defined in the iframe with specified 'iframeName'.
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

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
Get and Modify content of an Iframe

Last accessed pages

  1. Get the value of the selected /checked checkboxes in a form (42018)
  2. SHA1 Encrypt data in JavaScript (14279)
  3. Inheritance - Parent class and Child class (2448)
  4. querySelector and querySelectorAll (12508)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (46947)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1584)
  2. Contact page - CoursesWeb (1547)
  3. Tabs effect with CSS (1541)
  4. PHP getElementById and getElementsByTagName (749)
  5. Get Attribute (ID, Class, Name, Title, Src) with jQuery (624)