用于检测浏览器宽度和高度的JS适用于Chrome和Safari,但不适用于IE9或FF9

Eri*_*ric 4 html javascript jquery cross-browser

是否有任何理由说明此代码可以在Safari和Chrome中使用但不能在IE或FF中使用?

JS

function load(){
    w.value = window.outerWidth;
    h.value = window.outerHeight;
}
Run Code Online (Sandbox Code Playgroud)

HTML

<input name="h" id="h" type="hidden" value="" />
<input name="w" id="w" type="hidden" value="" />
Run Code Online (Sandbox Code Playgroud)

单击此处查看整个页面的源代码

在此先感谢您的帮助.

编辑:我弄清楚出了什么问题.必须将以下内容添加到加载功能中

var width = document.getElementById("w");
var height = document.getElementById("h");
width.value = window.outerWidth;
height.value = window.outerHeight;
Run Code Online (Sandbox Code Playgroud)

Sha*_*oli 5

使用支持几乎所有浏览器的jQuery方法。

function load(){
    w.value = $(window).width();
    h.value = $(window).height();
}
Run Code Online (Sandbox Code Playgroud)

参考:width()height()


小智 5

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

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