2010-06-10 26 views
29

Bu beni sinir ediyor. Gerçekten basit bir şey olmalı ama IE'de çalışmamı sağlayamıyorum. Geçerli pencerenin yüksekliğini almak istiyorum: Kaydırma yüksekliği değil, belge yüksekliği değil, gerçek pencere yüksekliği. Kaydırma yüksekliğini veren undefined ve document.documentElement.clientHeight döndüren window.innerHeight denedim. tarayıcı etiketi ayrıştırılır sonra document.body.offsetWidth ve document.body.offsetHeight kullandığı kod yürütülmelidir olduğunuPencere yüksekliğini alın

cevap

68
IE 8 için

ve alt,

document.documentElement.offsetHeight; 

Yani çapraz tarayıcı kullanın deneyin:

var height = "innerHeight" in window 
       ? window.innerHeight 
       : document.documentElement.offsetHeight; 
+1

desteklemiyor Andy, Flippen Ay adam, işe yarıyor, teşekkürler –

+0

Ben 'window.innerHeight' IE9 + – vsync

+0

için ekliyorum Bu tekniği bir çerçeve tabanlı sayfada başarıyla kullanıyorum IE 8/9/10 ve Firefox da. Teşekkür ederim –

1

http://www.javascripter.net/faq/browserw.htm

Not.

Güncelleme: olmalıdır bu

<script type="text/javascript"> 
<!-- 

var viewportwidth; 
var viewportheight; 

// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight 

if (typeof window.innerWidth != 'undefined') 
{ 
     viewportwidth = window.innerWidth, 
     viewportheight = window.innerHeight 
} 

// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document) 

else if (typeof document.documentElement != 'undefined' 
    && typeof document.documentElement.clientWidth != 
    'undefined' && document.documentElement.clientWidth != 0) 
{ 
     viewportwidth = document.documentElement.clientWidth, 
     viewportheight = document.documentElement.clientHeight 
} 

// older versions of IE 

else 
{ 
     viewportwidth = document.getElementsByTagName('body')[0].clientWidth, 
     viewportheight = document.getElementsByTagName('body')[0].clientHeight 
} 
document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>'); 
//--> 
</script> 

Bulunan here

+0

Hayır, doesnt iş, kaydırma yüksekliği sağlar –

+0

@YO Momma: aşağıdaki kodu kullanabilirsiniz 'myWidth = screen.availWidth; myHeight = screen.availHeight; 'bu, tüm tarayıcılarda çalışır. bir şey window.innerheight IE – Raje

5

kullanıyorum: Get document height (cross-browser) - James Padolsey

Ve ayrıca jQuery aynı şeyi yapıyor bulundu:

doc = document; 
var theHeight = Math.max(
    doc.body.scrollHeight, doc.documentElement.scrollHeight, 
    doc.body.offsetHeight, doc.documentElement.offsetHeight, 
    doc.body.clientHeight, doc.documentElement.clientHeight 
); 

buradan Bulunan

// Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height], whichever is greatest 
// unfortunately, this causes bug #3838 in IE6/8 only, but there is currently no good, small way to fix it. 
return Math.max(
    elem.body[ "scroll" + name ], doc[ "scroll" + name ], 
    elem.body[ "offset" + name ], doc[ "offset" + name ], 
    doc[ "client" + name ] 
); 
+0

Yanıltıcı. "ScrollHeight", ekranda görünmeyen içerik de dahil olmak üzere bir öğenin içeriğinin yüksekliğini verdiğinden (https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight) bu yana, * * belge yüksekliği **. Ancak soru ** pencere yüksekliği ** elde etmekti. – Lightman

İlgili konular