Javascript Reference - HTML DOM IFrame src Property








The src attribute from iframe element specifies the URL of the document to show in an iframe.

The src property sets or gets the value of the src attribute in an iframe element.

Browser Support

src Yes Yes Yes Yes Yes

Syntax

Return the src property.

var v = iframeObject.src

Set the src property.

iframeObject.src=URL




Property Values

Value Description
URL Set the URL of the document to embed in the iframe.

Return Value

A String type value representing the URL to embed in the iframe.

Example

The following code shows how to get the URL of the document shown in an iframe.


<!DOCTYPE html>
<html>
<body>
<iframe id="myFrame" src="http://example"></iframe>
<p id="demo"></p>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--from  w  w w. ja  va 2 s .  com-->
    var x = document.getElementById("myFrame").src;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the URL of the document to embed in the iframe.


<!DOCTYPE html>
<html>
<body>
<iframe id="myFrame" src="http://example.com"></iframe>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--   w w  w . j  av  a  2  s.c om-->
    document.getElementById("myFrame").src = "http://www.cnn.com";
}
</script>
</body>
</html>

The code above is rendered as follows: