Javascript Course


IFrames are used for displaying multiple HTML documents in the same browser window. The iframe object correspond to the <iframe> HTML element.
To create iframe, you can use the fallowing HTML tag:

<iframe src='page_url' width='600' height='200' name='name_frame'></iframe>

• The window.frames property returns an array with all iframe elements in current window.
You can access an iframe using its index in the array (strting from 0) or using the value of the name attribute or its ID (with document.getElementById()).

When moving up the chain toward the parent from an iframe, you can use one of parent or top keyword.
- top refers to the top - most window object in the hierarchy, while parent means the immediate owner window.


The fallowing code shows how you can acces the first iframe in main window, using frames array, and an iframe by its 'name'.
<script>
//array with all iframes in main window
var iframes = top.frames;

//accesses the first iframe
var ifr1 = iframes[0];

//accesses an iframe by its name
var ifr1 = iframes[0]['iframe_name'];
</script>

- The following example displays in an HTML element the values of the 'src', 'height' and 'width' attributes of an iframe, accessed with getElementById().
<iframe src='/' width='600' height='200' id='ifr1'></iframe>
<p>Click on the button to display the values of the 'src', 'height' and 'width' attributes of the iframe.</p>
<button id='btn1'>Click</button>
<blockquote id='resp'></blockquote>

<script>
var ifr = document.getElementById('ifr1'); //the iframe

var resp = document.getElementById('resp');
document.getElementById('btn1').addEventListener('click', (ev)=>{
 resp.innerHTML ='src: '+ ifr.src +'<br>height: '+ ifr.height +'<br>width: '+ ifr.width;
});
</script>

iframe object properties

For each <iframe> tag in an HTML document, JS creates an iframe object, which contains specific properties.

allowPaymentRequest - Returns True or False, indicating whether the Payment Request API may be invoked on a cross-origin iframe.

contentDocument - Returns the document object generated by an iframe.
<h4>Example contentDocument</h4>
<iframe id='ifrm' src='/'></iframe>
<p>Click on the button to change the content of the iframe above.</p>
<button id='btn1'>Click</button>

<script>
document.getElementById('btn1').addEventListener('click', (ev)=>{
 var ifr = document.getElementById('ifrm');
 ifr.contentDocument.body.innerHTML ='<h3>Content from JS added in iframe</h3>';
});
</script>
contentWindow - Returns the window object generated by an iframe.
<h4>Example contentWindow</h4>
<iframe id='ifrm' src='/'></iframe>
<p>Click on the button to change the content of the iframe above.</p>
<button id='btn1'>Click</button>

<script>
document.getElementById('btn1').addEventListener('click', (ev)=>{
 var ifr = document.getElementById('ifrm');
 ifr.contentWindow.document.body.innerHTML ='<h3>Content from JS added in iframe</h3>';
});
</script>
height - the value of the height attribute in an iframe (in pixels).
<h4>Example iframe height</h4>
<iframe srcdoc='iframe content' id='ifrm' src='#'></iframe>
<p>Click on the button to change the iframe height.</p>
<button id='btn1'>Set height</button>

<script>
document.getElementById('btn1').addEventListener('click', (ev)=>{
 var ifr = document.getElementById('ifrm');
 ifr.height =255;
});
</script>
name - Gets and sets the name attribute.
<h4>Example iframe height</h4>
<iframe srcdoc='iframe content' id='ifrm' src='#' name='name_ifrm'></iframe>
<p>Click on the button to display the iframe name.</p>
<button id='btn1'>iframe neme</button> <span id='resp'></resp>

<script>
var resp = document.getElementById('resp');
document.getElementById('btn1').addEventListener('click', (ev)=>{
 var ifr = document.getElementById('ifrm');
 resp.innerHTML = ifr.name;
});
</script>
referrerPolicy - returns or sets the value of the referrerpolicy attribute.
The referrerpolicy attribute define which referrer is sent when fetching the resource. It can have these values:
- 'no-referrer' - the Referer: HTTP header will not be sent.
- 'origin' - the referrer will be the origin of the page (domain name).
- 'unsafe-url' - the referrer will include the origin and the path.
<h4>Example referrerPolicy</h4>
<iframe srcdoc='iframe content' id='ifrm' src='#' referrerpolicy='unsafe-url'></iframe>
<p>Click on the button to display the value of the 'referrerpolicy' attribute in iframe.</p>
<button id='btn1'>iframe referrerpolicy</button> <span id='resp'></resp>

<script>
var resp = document.getElementById('resp');
document.getElementById('btn1').addEventListener('click', (ev)=>{
 var ifr = document.getElementById('ifrm');
 resp.innerHTML = ifr.referrerPolicy;
});
</script>
src - sets or returns the value of the src attribute in an iframe.
<h4>Example set iframe src</h4>
<iframe id='ifrm' src='/' width='455'></iframe>
<p>If you click on the button, it sets another value for the iframe 'src' attribute, that will display another page in iframe.</p>
<button id='btn1'>set iframe src</button>

<script>
document.getElementById('btn1').addEventListener('click', (ev)=>{
 var ifr = document.getElementById('ifrm');
 ifr.src ='//gamv.eu/';
});
</script>
srcdoc - sets or returns the value of the 'srcdoc' attribute, that represents the content to display in the iframe.
<h4>Example srcdoc</h4>
<iframe srcdoc='iframe content' id='ifrm' src='#'></iframe>
<p>Click on the button to change the iframe content with 'srcdoc' property.</p>
<button id='btn1'>Set srcdoc</button>

<script>
document.getElementById('btn1').addEventListener('click', (ev)=>{
 var ifr = document.getElementById('ifrm');
 ifr.srcdoc ='<h3>Content added with srcdoc from JavaScript.</h3>';
});
</script>
width - the value of the width attribute in an iframe (in pixels).
<h4>Example iframe width</h4>
<iframe srcdoc='iframe content' id='ifrm' src='#'></iframe>
<p>Click on the button to change the iframe width.</p>
<button id='btn1'>Set width</button>

<script>
document.getElementById('btn1').addEventListener('click', (ev)=>{
 var ifr = document.getElementById('ifrm');
 ifr.width =555;
});
</script>

iframe navigation

With the names of the iframe elements we can exchange information between two iframes, throught the main window, for example, we can make a link in a iframe to open a page in another iframe.
Next we will study three cases of relationships and visits between iframes and main window.

The contents of a document can be accessed from another document only if the two documents are located in the same domain.
The iframe document can be accessed with the contentDocument property.


  1) The main window accessing an iframe
If you have a script in the parent window, to access the iframe, just use the name of the iframe.
For example:
window.frames['ifr_name'].contentDocument.write('Message from parent window');
- The text will be write in the iframe with name 'ifr_name'.

  2) The iframe accessing the main window
If we have the JS script in an iframe, and we want that script to access elements of main window, we use the top keyword in the script inside iframe.
For example:
top.document.write('Message from iframe');
- The text will be write in the main window.

  3) The iframe accessing another iframe
If we have a JS script in an iframe (iframe1), and we want that script to access another iframe elements (iframe2), we use the top keyword and the name of the second iframe.
For example:
top.iframe2.contentDocument.write('Message from iframe1')
- The text will be write in the iframe2 page.

• If you want to change the contents of a iframe (named 'iframe1') from main window or from another iframe, you could create a link like this:
<a href='new_page.html' target='iframe1'>New Page</a>
- Or, using JavaScript:
top.frames['iframe1'].src ='newpage.html';

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
iframe object

Last accessed pages

  1. Node.js Move and Copy file (28419)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141747)
  3. Rectangle, Oval, Polygon - Star (3322)
  4. PHP PDO - prepare and execute (9187)
  5. jQuery Ajax - load() method (10835)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (471)
  2. CSS cursor property - Custom Cursors (78)
  3. The Mastery of Love (69)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)