admin管理员组

文章数量:1320621

I am trying to alert the size of the window and the size of an element in order to put the element where I want it to be.

<script type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function(){
    var element=$(".element").width();
    var window=$(window).width();
    alert("Element: "+element+" "+"Window: "+window);
  });
</script>

When I open it in the browser, I get the width of the element but the width of the window is null. What am I doing wrong?

I am trying to alert the size of the window and the size of an element in order to put the element where I want it to be.

<script type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function(){
    var element=$(".element").width();
    var window=$(window).width();
    alert("Element: "+element+" "+"Window: "+window);
  });
</script>

When I open it in the browser, I get the width of the element but the width of the window is null. What am I doing wrong?

Share Improve this question edited Jul 5, 2012 at 0:55 balexandre 75.1k47 gold badges238 silver badges351 bronze badges asked Jul 5, 2012 at 0:49 user181275user181275 131 gold badge1 silver badge6 bronze badges 2
  • 1 don't use reserved words as variables, window is already an object. – balexandre Commented Jul 5, 2012 at 0:54
  • Thank you, I did not notice that. – user181275 Commented Jul 5, 2012 at 0:57
Add a ment  | 

1 Answer 1

Reset to default 13

You're shadowing the window global variable. Rename your variable:

var windowWidth = $( window ).width();

本文标签: javascriptWhy is the width of the window nullStack Overflow