• 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.
contentWindow
property, it returns the window object of a specified iframe.<html> <head><title>Iframe page</title></head> <body> Content to be displayed in iframe ... </body> </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>
Another way to access the content of an iframe is to use thetop.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.
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
#id { font-weight: 800; }
function someFunction() { alert("CoursesWeb.net"); } setInterval("someFunction()", 2000);
$vname = 8; echo $vname;