2010-08-31 4 views
0

J'utilise le code suivant pour obtenir la largeur de la fenêtre et la hauteurjavascript pauses taille de la fenêtre lors du démarrage

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; 
} 

Je l'appelle javascript lors de la charge de page. Si je garde mon URL comme page d'accueil et essaie d'ouvrir l'explorateur, je vois la plupart du temps, ces fonctions me donnent des valeurs égales à 0.

Une idée pour y remédier? Mon problème est, ces fonctions fonctionnent toujours mais si j'appelle ces fonctions dans la page d'accueil d'IE, elles renvoient 0 (1 dans 4 cas).

Y a-t-il une meilleure façon d'obtenir la largeur d'une fenêtre?

+0

Je ne comprends pas. Que voulez-vous dire par "pendant le chargement de la page" exactement? Quand ouvrez-vous quel explorateur? –

Répondre

0

Si peut aider i utiliser cette fonction qui est plus crossbrowser et vous pouvez l'appeler la charge de l'événement comme michael suggéré

// 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; 
} 
0

Ensuite, si la question est la page encore le chargement, alors peut-être vous devriez attendre jusqu'à ce que des thats termine avant d'exécuter votre code:

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