Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery(window).height inaccurate

Tags:

jquery

I'm doing a simple test on $(window).height() and I'm getting a value of 2602 on a screen resolution of 1366 x 768.

jQuery(document).ready(function($){

    var W = $(window).width();
    var H = $(window).height();

    console.log('W ' + W);
    console.log('H ' + H);

}

Outputs:

W 1226 H 2602

Any tips on how to debug this or what I'm doing wrong?

EDIT: Using chrome and FF

I'm literally typing this in console: jQuery(window).height();

like image 388
AlxVallejo Avatar asked Aug 12 '13 02:08

AlxVallejo


4 Answers

This can be just as simple as a missing or incorrect DOCTYPE (affected Chrome but not IE).

Interestingly this isn't even a jQuery issue - it affects document.documentElement.clientHeight too.

Without doctype:

enter image description here

With doctype:

<!DOCTYPE html>

enter image description here

I thought I had a layout template on my page, but it turned out I didn't - and therefore didn't have DOCTYPE.

like image 168
Simon_Weaver Avatar answered Nov 15 '22 21:11

Simon_Weaver


You're doing everything right, and you're not getting the values you should. This is not how jQuery is supposed to behave.

In this very page, I tested out the functions you were calling to see what their values would be. They did represent accurately the actual width and height of the visible page in my window. Here's the results.

Chrome, with the standard Dev console

Some calls to the window width and height in jQuery, returning correct results for pixels on screen

Firefox, with Firebug

Some calls to the window width and height in jQuery, returning correct results for pixels on screen

Something is very wrong on your end

jQuery's $(window).height() is supposed to display the visible viewport. See the docs on jQuery's height function:

This method is also able to find the height of the window and document.

$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document 

window.height is a settable value things can interfere with, but $(window).height() is unaffected by changes to it.

What's going wrong exactly?

Whatever is going on, whatever function you're calling, you are not calling jQuery's implementation of $(window).height().

Using your browser's developer console, put a break point on the line where you call $(window).height() and step into the function call. What is actually being called when you call $(window).height()? You need to find this out.

Has someone replaced the height function with a new or better one?

A strong suspicion of mine is that some part of the code base you're working with has gone and done this:

// Replace jQuery's height function with a better one to fix bug XYZ
$(window).height = function brandNewHeightFunction() {
    return someVeryWrongValue;
};

If so, when you step into the height function, it may in fact not enter into a part of the jQuery library, but another part of your own libraries. Find out what necessitated it and see what you can do about it. If you simply remove this functionality, other stuff depending on it working this way may start breaking.

Has someone gone and modified/customised your jQuery library?

Check to see if this has happened with whoever you can, and whatever history tools you have available. What version of jQuery are you using? Download the same one from the jQuery site if possible, and see the code you see when you step into the window height function is the same one as you see there.

Redownload jQuery and reference it. If your window height function suddenly starts working, someone may have done this. Find out why this was customised. If you repair it, other stuff depending on this behaviour might start breaking.

like image 40
doppelgreener Avatar answered Nov 15 '22 22:11

doppelgreener


Actually, this was a result of a hidden div element on my page. I had a thickbox link that would pull this hidden content. Removing this fixed the jQuery.width()!

Surprising, not sure if helpful, but is the answer indeed.

like image 43
AlxVallejo Avatar answered Nov 15 '22 21:11

AlxVallejo


Same problem here, it looks that jquery (and dom in general) is not calculating sizes correctly in quirksmode.

Two solutions:

  1. Add doctype html at the top of your page, or
  2. Use window.innerHeight, window.innerWidth if first option is not an option.

Hope it helps.

like image 43
Lex Avatar answered Nov 15 '22 23:11

Lex