为什么这段代码可以在 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/