用于检测浏览器宽度和高度的JS可在Chrome& Safari,但不支持IE9或FF9 [英] JS to detect browser width and height works in Chrome & Safari but not IE9 or FF9

查看:130
本文介绍了用于检测浏览器宽度和高度的JS可在Chrome& Safari,但不支持IE9或FF9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么理由可以使此代码在Safari和Chrome中运行,而不能在IE或FF中运行?

Is there any reason why this code would work in Safari and Chrome but not IE or FF?

JS

function load(){
    w.value = window.outerWidth;
    h.value = window.outerHeight;
}

HTML

<input name="h" id="h" type="hidden" value="" />
<input name="w" id="w" type="hidden" value="" />

单击此处以获取整个页面的源代码

预先感谢您的帮助.

我发现了什么地方出了问题.必须将以下内容添加到加载功能

I figured out what was wrong. The following had to be added to the load function

var width = document.getElementById("w");
var height = document.getElementById("h");
width.value = window.outerWidth;
height.value = window.outerHeight;

推荐答案

Internet Explorer使用不同的属性来存储窗口大小.进入Internet Explorer clientWidth/Height是内部窗口的大小(减去滚动条,菜单等使用的像素),因此请尝试

Internet Explorer use different properties to store the window size. Into Internet Explorer clientWidth/Height is inner window size (subtract the pixels used by scroll bar, menu etc) so try

function load(){
  if(window.outerHeight){
      w.value = window.outerWidth;
      h.value = window.outerHeight;
  }
  else {
      w.value = document.body.clientWidth;
      h.value = document.body.clientHeight; 
  }
}

这篇关于用于检测浏览器宽度和高度的JS可在Chrome&amp; Safari,但不支持IE9或FF9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆