admin管理员组文章数量:1221766
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="" />
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;
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="" />
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;
Share
Improve this question
edited Feb 7, 2012 at 20:02
Eric
asked Feb 7, 2012 at 19:47
EricEric
5751 gold badge8 silver badges25 bronze badges
1
- 1 What doesn't work about it? What is it doing, what is it not doing, what errors are being thrown, etc. – Kevin B Commented Feb 7, 2012 at 19:50
3 Answers
Reset to default 6Internet 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;
}
}
Use jQuery methods which support almost all the browsers.
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. window.outerWidth
is not supported in IE 8 and below.
Here are some good docs for 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')
.
本文标签:
版权声明:本文标题:javascript - JS to detect browser width and height works in Chrome & Safari but not IE9 or FF9 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739282532a2156311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论