javascript - 用于检测浏览器宽度和高度的 JS 在 Chrome 和 Safari 中有效,但在 IE9 或 FF9 中无效

标签 javascript jquery html cross-browser

为什么这段代码可以在 Safari 和 Chrome 中运行但不能在 IE 或 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="" />

Click here for the entire page's source code

在此先感谢您的帮助。

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

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

最佳答案

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; 
  }
}

关于javascript - 用于检测浏览器宽度和高度的 JS 在 Chrome 和 Safari 中有效,但在 IE9 或 FF9 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9182811/

相关文章:

javascript - 使用带有折叠菜单的 jQuery

jquery - 不同尺寸图像的 Owl Carousel ?

javascript - WTForms-Javascript : pass onclick to WTF field

jquery - 表比模态体大

html - 使用 HTML 和 CSS 将背景图像与图像对齐

javascript - 捕获和处理videojs的 'click'的 'bigplaybutton'事件不起作用

javascript - onmousedown 调用鼠标捕获函数?

javascript - Aurelia 使用 ES5 计算属性

javascript - 使用ontouchend,禁用onclick

jquery - HighCharts/jQuery : chart. 重绘速度慢 - 可以提高速度吗?