Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Высота всей страницы(вместе с невидимой частью) (https://javascript.ru/forum/misc/1166-vysota-vsejj-stranicy-vmeste-s-nevidimojj-chastyu.html)

lancer 18.04.2008 21:27

Высота всей страницы(вместе с невидимой частью)
 
Дано:
Страница большой высоты. Т.к. вся не помещается в окне браузера, имеется вертикальный скролл.

Доктайп XHTML Strict 1.0
В CSS имеем:
Код:

html,body{height:100%}

Найти:
Вывести высоту страницы в px.


Как я пытался:
1.alert(document.getElementById('html').offsetHeig ht)
2.alert(document.getElementById('html').clientHeig ht)

В обоих случаях выводится результат равный высоте окна браузера.
Как я думаю, из-за html:100%.

Вопрос: Как же узнать высоту всей страницы, а не только видимой части?

Kolyaj 18.04.2008 22:24

lancer,
var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera')  > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);

function getDocumentHeight() {
  return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}

function getViewportHeight() {
  return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}

lancer 18.04.2008 23:06

Kolyaj, спасибо. Выручил.

Gamler 21.04.2009 13:23

Цитата:

Сообщение от Kolyaj (Сообщение 2061)
lancer,
var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera')  > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);

function getDocumentHeight() {
  return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}

function getViewportHeight() {
  return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}

В IE этот код неработет!
Что можна придумать с Explorer'ом?

Kolyaj 21.04.2009 13:24

Цитата:

Сообщение от Gamler
В IE этот код неработет!

Отличный багрепорт, чувак. Могу только сказать, что все работает.

vahrusha 08.08.2010 14:16

Отличный рабочий способ.
Для аналогичного поиска ширины, в функции надо все "Height" заменить на "Width".
ЗЫ: пишу, потому что до меня самого доходило это около часа.
:)

andrewstezenk 05.03.2016 00:00

Всё прекрасно работает в хроме, по крайней мере)


Часовой пояс GMT +3, время: 09:58.