简体   繁体   English

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

[英]JS to detect browser width and height works in Chrome & Safari but not IE9 or FF9

Is there any reason why this code would work in Safari and Chrome but not IE or FF?这段代码是否可以在 Safari 和 Chrome 中运行,但不能在 IE 或 FF 中运行?

JS JS

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

HTML 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点击这里查看整个页面的源代码

Thanks in advance for the assistance.提前感谢您的帮助。

Edit: 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 use different properties to store the window size. Internet Explorer 使用不同的属性来存储窗口大小。 Into Internet Explorer clientWidth/Height is inner window size (subtract the pixels used by scroll bar, menu etc) so try进入 Internet Explorer 客户端宽度/高度是内部窗口大小(减去滚动条、菜单等使用的像素)所以尝试

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

Use jQuery methods which support almost all the browsers.使用支持几乎所有浏览器的 jQuery 方法。

function load(){
    w.value = $(window).width();
    h.value = $(window).height();
}

Reference: width() , height()参考:宽度()高度()

If you are using jQuery then I suggest using .width() and .height() because they will function properly cross-browser.如果您使用的是 jQuery,那么我建议使用.width().height()因为它们可以跨浏览器正常工作。 window.outerWidth is not supported in IE 8 and below. IE 8 及更低版本不支持window.outerWidth

Here are some good docs for window.outerWidth : https://developer.mozilla.org/en/DOM/window.outerWidth以下是window.outerWidth的一些好的文档: https ://developer.mozilla.org/en/DOM/window.outerWidth

Using .width() and .height() will return a unit-less number (that is in pixels), if you want to get the exact height/width set (including px or em ) then you can use .css('width') and .css('height') .使用.width().height()将返回一个无单位的数字(以像素为单位),如果你想获得确切的高度/宽度集(包括pxem ),那么你可以使用.css('width').css('height')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用jQuery的Submit按钮在IE9和FF9中不起作用 - Submit button using jQuery not working in IE9 and FF9 JS函数在FF和Safari中效果很好,但在IE9中花费很长时间 - JS function works great in FF and Safari, but takes a VERY long time in IE9 Javascript适用于FF / IE,但不适用于Chrome / Safari - Javascript works in FF / IE but not Chrome / Safari 我的 Javascript 适用于 IE FF 和 Safari,但不适用于 Chrome - My Javascript works on IE FF & Safari, but not Chrome JavaScript浏览器问题在FF中有效,但在IE和Chrome中无效 - javascript browser problem works in FF but not in IE and Chrome 在IE9中未检测到视图模型功能绑定,但在FF和Chrome中有效 - View Model Function binding not detected in IE9 but works in FF and Chrome Canvas 图像无法在 Chrome 中呈现(适用于 FF4 和 IE9) - Canvas Image not rendering in Chrome (works in FF4 & IE9) IE9 / FF / Chrome的Javascript / JQuery浏览器测试框架 - Javascript/JQuery browser testing frame work for IE9/FF/Chrome 显示隐藏的div,为什么代码可以在FF / Chrome中工作,但不能在IE9中工作? - Show hidden div, why code works in FF/Chrome but NOT in IE9? 为什么Chrome在我的JS上抛出错误,但是在IE FF Android和Safari上可以正常工作? - Why is chrome throwing error on my JS but works fine in IE FF Android and Safari?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM