4

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();

});
Dshiz
  • 3,099
  • 3
  • 26
  • 53

2 Answers2

6

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.

mask8
  • 3,560
  • 25
  • 34
  • I wasn't sure why either.. It worked right away with no problems.. Thanks for the help.. – Dshiz Aug 18 '12 at 02:19
0

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

flap13
  • 379
  • 3
  • 14