Javascript Reference - HTML DOM IFrame width Property








The width attribute from <iframe> element specifies the width of an iframe.

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

Browser Support

width Yes Yes Yes Yes Yes

Syntax

Return the width property.

var v = iframeObject.width

Set the width property.

iframeObject.width=pixels




Property Values

Value Description
pixels The width in pixels

Return Value

A String type value representing the width of the iframe in pixels

Example

The following code shows how to change the width of an iframe.


<!DOCTYPE html>
<html>
<body>
<iframe id="myFrame" src="http://example.com" height="200" width="250"></iframe>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--  w  w  w .  j a  v  a 2 s  .  c o m-->
    document.getElementById("myFrame").width = "400";
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the width of an iframe.


<!DOCTYPE html>
<html>
<body>
<iframe id="myFrame" src="http://example.com" height="200" width="250"></iframe>
<p id="demo"></p>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--from  w  w  w .j a v  a2 s . co m-->
    var x = document.getElementById("myFrame").width;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows: