codingJoe
codingJoe

Reputation: 4953

javascript: $(window).height is not a function

I have some javascript that I am using to resize my background image to fit my window. There are some confusing things going on that I just don't get.

  1. Firebug and I assume my page doesn't recognize my resizeFrame function unless I place it below the body block. Why?

  2. Why am I getting the error: $(window).height is not a function?

Any suggestions or insights would be helpful.

<!-- This this placed in <head> block -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 

<script> <!-- This placed below body block -->
  jQuery.event.add(window, "load", resizeFrame);
  jQuery.event.add(window, "resize", resizeFrame);

  function resizeFrame() 
  {
      var h = $(window).height();
      var w = $(window).width();
      $('body').css('background-size', w + 'px ' + h + 'px' );
  }  
</script>

Upvotes: 1

Views: 9491

Answers (2)

Pawel
Pawel

Reputation: 11

if you're developing under wordpress you should use jquery() instead of $(). In your code you've got

jQuery.event.add(window, "resize", resizeFrame);

and

var h = $(window).height();

that's why you got a mistake

Upvotes: 1

Jivings
Jivings

Reputation: 23262

You have the answer. window.height is not a function.

You want to change $(window).height() to window.screen.height to get the value.

Same with width.

Upvotes: 3

Related Questions