【问题标题】:Get browser height from within a frame从框架内获取浏览器高度
【发布时间】:2012-08-17 12:28:53
【问题描述】:

我正在使用 Moodle:

我正在尝试从框架内运行一些 Javascript 来获取浏览器窗口的高度,因此如果用户使用的浏览器尺寸不够理想,我可以提醒他们。

我试过这样的事情:

window.top.document.body.offsetHeight
document.documentElement.clientHeight
$(document).height()
$(window).height()

但它们都给出了静态高度,当我调整浏览器大小时,它们永远不会改变。

哦,它必须在 IE8 中工作。

【问题讨论】:

  • which never change when I resize the browser....这是一个响应式架构,您需要在$(window).on('resize', function(){ console.log($(window).height()); });的范围内
  • 我的意思是,在刷新浏览器之间。我已经设置了一个警报来告诉我负载的高度。

标签: javascript jquery height


【解决方案1】:

你必须使用 iFrame 窗口的parent 属性:

在 iFrame 中运行此代码,它将返回父窗口的高度:

$(window.parent).height();

但是,如果您使用的是 jQuery 1.8.0,如果您的浏览器位于 quirks mode 中,这可能无法正常工作。从 jQuery 1.8.0 开始,命令 $(window).height() 停止在 quirks 模式下对 Internet Explorer 工作,而他们 don't plan to fix it。

如果您使用的是 jQuery 1.8.0,请改用这个细微的变化:

$(window.parent.document).height();

这将确保跨浏览器支持,即使在 IE 怪癖模式下也是如此;)

http://www.w3schools.com/jsref/prop_win_parent.asp

【讨论】:

    【解决方案2】:

    一个简单的解决方案是从 iframe 返回文档高度调用父文档函数

    Iframe Javascript:

    alert(top.getDocHeight());  
    

    主文档 Javascript:

    function getDocHeight()
    {
        return $(window).height();
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      相关资源
      最近更新 更多