Open In App

HTML | DOM IFrame Object

The IFrame Object property in HTML DOM is used to create and access the <iframe> element within the object. An inline frame is used for embedding another document within the current HTML document. 

Syntax:



var x = document.getElementById("myframe");
var x = document.createElement("IFRAME");

Property Values:

Example 1: This example describes the getElementById() method to access <iframe> element. 






<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM IFrame Object 
    </title>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
     
    <h2>DOM IFrame Object Property</h2>
     
    <button onclick = "myGeeks()">
        Click Here!
    </button>
     
    <br><br>
 
     
    <iframe id = "GFGFrame" src =
        width = "400" height = "200">
    </iframe>
     
    <p id = "GFG"></p>
     
    <!-- script to access iframe element -->
    <script>
        function myGeeks() {
            var x = document.getElementById("GFGFrame").src;
            document.getElementById("GFG").innerHTML = x;
        }
    </script>
</body>
 
</html>                   

Output: Before Click on the button:

  

After Click on the button:

  

Example 2: This example describes the document.createElement() method to create <iframe> element. 




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM IFrame Object
    </title>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
     
    <h2>DOM IFrame Object Property</h2>
     
    <button onclick = "myGeeks()">
        Click Here!
    </button>
 
    <br><br>
     
    <!-- script to create iframe element -->
    <script>
        function myGeeks() {
             
            /* Create iframe element */
            var ifram = document.createElement("IFRAME");
             
            /* Set the source attribute */
            ifram.setAttribute("src",
                    "https://ide.geeksforgeeks.org/tryit.php");
             
            /* Set the iframe height */
            ifram.setAttribute("height", "200");
             
            /* Set the iframe width */
            ifram.setAttribute("width", "400");
             
            document.body.appendChild(ifram);
        }    
    </script>
</body>
 
</html>                   

Output: Before Click on the button:

  

After Click on the button:

 

Supported Browsers: The browser supported by DOM IFrame Object property are listed below:


Article Tags :