Alvaro
Alvaro

Reputation: 41595

Inconsistent results offered by $(window).height();

I'm experiment an inconsistent result trying to get the height of the browser's window.

To do it, I am using $(window).height(); on document ready and I've noticed I get different values (with 15px of difference) between cases. It seems that the results get influenced by the style sheet I am applying. Deleting the style sheet solve the problem (or just removing a couple of lines (such as font-size: 2em; , float:left; position:absolute;... things which doesn't seem to be related at all)

I believe styles should not be related with the browser's window size at all. Shouldn't it be returning exactly the same value for any case?

I've read the documentation of $.height() and it doesn't say anything about this topic.

I've also noticed it returns the correct dimensions when called on load like so:

$(window).on('load', function() {
    var windowHeight = $(window).height();
    console.log(windowHeight);
});

I'm sorry but I couldn't reproduce my problem at jsfiddle.

Upvotes: 1

Views: 295

Answers (2)

Rotem
Rotem

Reputation: 2356

It may got to do with the scrollbar. After all the content was loaded you may have a scrollbar that can reduce the output by 15px.

Upvotes: 1

SajithNair
SajithNair

Reputation: 3867

$(window).height(); will never change by font or float in css. It looks like you are adjusting your javascript control window which is causing the size to vary. Please dont adjust the console size, then you will have fixed window size.

Upvotes: 0

Related Questions