admin管理员组

文章数量:1391975

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=".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>

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./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>
Share Improve this question asked Feb 4, 2012 at 18:01 codingJoecodingJoe 4,9539 gold badges49 silver badges61 bronze badges 4
  • I would guess it's not recognised because it has the error. Fix that first. – Jivings Commented Feb 4, 2012 at 18:05
  • 1 Why are you directly using jQuery.event.add instead of $(window).on('load', resizeFrame); – loganfsmyth Commented Feb 4, 2012 at 18:09
  • How can resizeFrame be referenced before it is defined? – ori Commented Feb 4, 2012 at 18:29
  • Getting another error$(window).on is not a function in $(window).on('load', resizeFrame); – codingJoe Commented Feb 6, 2012 at 1:19
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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

本文标签: jqueryjavascript (window)height is not a functionStack Overflow