Dshiz
Dshiz

Reputation: 3331

Why doesn't $(window).height(); return a value, but $(document).height(); does?

I'm trying to use jQuery to get current window height. I intend to set a variable with this value, and update the value upon resize. For some reason, $(window).height(); always returns zero, but $(document).height(); returns a value. Why would this be?

(code snipped for brevity)

$(document).ready(function () {

function drawGrid() {

    var context = document.getElementById("gridCanvas").getContext("2d");

    var height = $(window).height();
    var width = height/2/8;
    alert(height);

    $(window).resize(function () {
        // do some stuff
    });

        // do some cool drawing stuff

    }

    drawGrid();

});

Upvotes: 4

Views: 2762

Answers (2)

flap13
flap13

Reputation: 411

$(window).height() and also $(window).width() return 0 in IE when in compatibility mode. try to use $(document).height() or $(document).width() instead.

Upvotes: 0

mask8
mask8

Reputation: 3638

can you try

$(window).load(function () { 

instead of

$(document).ready(function () { 

and see if it works. I think you have to wait window finish loading if you want to get window's size.

Upvotes: 6

Related Questions