Javascript 如何获取当前页面的Iframe大小
我有一个带有Iframe的页面。我使用以下代码获取Iframe的大小:Javascript 如何获取当前页面的Iframe大小,javascript,html,iframe,Javascript,Html,Iframe,我有一个带有Iframe的页面。我使用以下代码获取Iframe的大小: <script language="javascript"> function sn() { alert('height is now '+document.getElementById("Iframemain").height) alert('height is now '+document.getElementById("Iframemain").width) } </script> <i
<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">
函数sn()
{
警报(‘高度现在是’+document.getElementById(“Iframemain”).height)
警报('高度现在为'+document.getElementById(“Iframemain”).width)
}
我还有另一个带有Iframe的页面,但我不知道Iframe的id。我想知道使用javascript的Iframe的大小。我怎么做
提前准备好
塔克斯·罗里·麦克罗森
我也尝试过下面的代码,但在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>
函数sn()
{
var frame=window.frames[0];
警报(帧宽度)
警报(帧高度)
}
如果您不知道框架的ID,并且它是页面上唯一的框架/iframe,则可以使用以下代码:
var frame = window.frames[0];
alert('height is now ' + frame.height)
alert('width is now ' + frame.width)
如果它不是唯一的框架,您需要根据需要更改
窗口。框架[索引]
。跨域脚本编写非常困难。即使是像获取高度和宽度这样简单的事情也很难做到。查看前面的堆栈溢出问题,以获得全面的解决方案。这两个页面是否使用同一个域?否,它们位于不同的域上,请添加到您的答案中;如果框架包含name
属性,则也可以使用window.frames['name']