[英]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;
}
}
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()
将返回一个无单位的数字(以像素为单位),如果你想获得确切的高度/宽度集(包括px
或em
),那么你可以使用.css('width')
和.css('height')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.