javascript窗口大小在启动期间中断

javascript窗口大小在启动期间中断,javascript,Javascript,我使用以下代码来获取窗口的宽度和高度 function pageWidth() { return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.bo

我使用以下代码来获取窗口的宽度和高度

function pageWidth() {
    return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}
function pageHeight() {
    return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
}
我在页面加载期间调用这个javascript。如果我将我的URL作为主页,并尝试打开资源管理器,我会看到大多数情况下,这些函数给我的值等于0

知道怎么修吗?我的问题是,这些函数总是有效的,但如果我在IE主页中调用这些函数,它们将返回0(4种情况下为1)


有没有更好的方法来获取窗口宽度?

如果问题是页面仍在加载,那么您可能应该等到加载完成后再运行代码:

window.addEventListener('load', function(){
  // ok, now good to call those functions and get values back!
}, false);

如果可以帮助我使用这个更交叉的函数,您可以在事件加载中调用它,如建议的那样michael

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
//
function getPageSize(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {  
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }   

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){  
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
}
//getPageSize()
//返回具有页面宽度、高度和窗口宽度、高度的数组
//核心代码来自-quirksmode.org
//
函数getPageSize(){
var xScroll,yScroll;
如果(window.innerHeight&&window.scrollMaxY){
xScroll=document.body.scrollWidth;
yScroll=window.innerHeight+window.scrollMaxY;
}else if(document.body.scrollHeight>document.body.offsetHeight){//all,但不包括Explorer Mac
xScroll=document.body.scrollWidth;
yScroll=document.body.scrollHeight;
}否则{//explorerMac…也可以在Explorer6、Mozilla和Safari中使用
xScroll=document.body.offsetWidth;
yScroll=document.body.offsetHeight;
}
var窗口宽度、窗口高度;
if(self.innerHeight){//除Explorer之外的所有
windowWidth=self.innerWidth;
windowHeight=self.innerHeight;
}else if(document.documentElement&&document.documentElement.clientHeight){//Explorer 6严格模式
windowWidth=document.documentElement.clientWidth;
windowHeight=document.documentElement.clientHeight;
}else if(document.body){//其他探索者
windowWidth=document.body.clientWidth;
windowHeight=document.body.clientHeight;
}   
//对于总高度小于视口高度的小页面
if(yScroll<窗高){
页面高度=窗口高度;
}否则{
pageHeight=yScroll;
}
//对于总宽度小于视口宽度的小页面
如果(xScroll
我不明白。你所说的“页面加载期间”到底是什么意思?你什么时候打开哪个浏览器?