2010-06-10 2 views
29

실망 스럽습니다. 정말 간단해야하지만 IE에서 작동하도록 할 수는 없습니다. 나는 현재 창 높이를 원한다 : 스크롤 높이가 아니라 문서 높이가 아니라 실제 창 높이. 나는 undefineddocument.documentElement.clientHeight을 반환하는 window.innerHeight을 시도하여 스크롤 높이를 제공합니다. 브라우저가 태그를 구문 분석 후 document.body.offsetWidthdocument.body.offsetHeight를 사용하는 코드가 실행되어야 함을창문 높이 받기

답변

68
IE 8

낮은,

document.documentElement.offsetHeight; 

그래서 크로스 브라우저를 사용하십시오 :

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

앤디, Flippen 아이 남자, 고마워요, 작동 –

+0

그 window.innerHeight은 IE9에 대한 것입니다 + – vsync

+0

나는 프레임 기반의 페이지 에서이 기술을 성공적으로 사용하고 있습니다 IE 8/9/10 및 Firefox에서도 마찬가지입니다. 고마워요. –

1

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

참고.

업데이트 : 해야이

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

찾을 수 here

+0

아니, 나던 작업을 스크롤 높이를 제공합니다 –

+0

@YO 엄마 : 다음 코드에서 사용할 수 있습니다. myWidth = screen.availWidth; myHeight = screen.availHeight; 이것은 모든 브라우저에서 작동합니다. 그 중 하나는 window.innerheight IE에서 지원하지 않습니다 – Raje

5

내가 사용 : Get document height (cross-browser) - James Padolsey

을 또한 jQuery이 같은 일을하고있는 것을 발견 :

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

여기에서 발견

// 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

오해의 소지가 있습니다. 'scrollHeight'는 [화면에 보이지 않는 내용을 포함한 요소의 내용 높이] (https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight)를 제공하기 때문에 * * 문서 높이 **. 그러나 질문은 창 높이 **를 얻는 것이 었습니다. – Lightman