2

I have a page with an Iframe. I am using the following code to get the size of Iframe:

<script language="javascript">
function sn()
{

alert('height is now '+document.getElementById("Iframemain").height)
alert('height is now '+document.getElementById("Iframemain").width)
}
</script>
<input type="button" onClick="sn()" value="Show height/width">

I have also another page with an Iframe but i do not know the id of the Iframe. I want to know the size the of the Iframe using javascript. How can i do it?

thanx in advance .

Thanx Rory McCrossan i have also tried that following code but getting udefined in alert msg.

<script language="javascript"> function sn() 
{

var frame = window.frames[0];
 alert(frame.width) 
 alert(frame.height)

 } 
</script>
 <input type="button" onClick="sn()" value="Show height/width"> 
</html>
Sameer
  • 372
  • 3
  • 16

2 Answers2

1

If you don't know the ID of the frame, and it is the only frame/iframe on the page you can use the following code:

var frame = window.frames[0];
alert('height is now ' + frame.height)
alert('width is now ' + frame.width)

If it is not the only frame, you'll need to change the window.frames[index] as required.

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
0

Cross-Domain scripting is very difficult. Even something simple like getting the height and width is hard. Check out this earlier stack overflow question for a comprehensive solution.

Community
  • 1
  • 1
Tin Can
  • 1,790
  • 2
  • 24
  • 30