IE 7/8检查当前窗口大小的正确语法是什么

时间:2012-02-17 03:42:52

标签: javascript internet-explorer prototype

任何人都可以建议如何确定即7/8的当前窗口大小,或者通常我需要计算当前宽度以便重定向到iframe到目前为止我已经尝试过:

document.documentElement.offsetWidth - 仅适用于ie9

document.body.offsetWidth - undefined

self.innerWidth - undefined

也许任何人都可以建议原型方法?

谢谢你们!

   if(document.documentElement.offsetWidth > (old_w + new_w)){
     window.location = '/mylocation';
   }

1 个答案:

答案 0 :(得分:2)

试试这个......正如我所研究的那样,这是一个跨浏览器实现你正在寻找的东西:

window.$getView = function() {
        var ret = {
            width : 0,
            height : 0,
            element : null
        };
        if( typeof window.innerWidth != 'undefined') {
            ret.width = window.innerWidth;
            ret.height = window.innerHeight;
            ret.element = window;
        } else if( typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
            ret.width = document.documentElement.clientWidth;
            ret.height = document.documentElement.clientHeight;
            ret.element = document.documentElement;
        } else {
            ret.width = document.getElementsByTagName('body')[0].clientWidth;
            ret.height = document.getElementsByTagName('body')[0].clientHeight;
            ret.element = document.getElementsByTagName('body')[0];
        }
        return ret;
    }