jQuery HTML Element How to - Get window width








Question

We would like to know how to get window width.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from  www  . j  a v a 2s  .  c  om-->
    $(document).ready(function(){
        console.log($(window).width());
        if ($(window).width() > 1000) {
            console.log("detected larger than 1000px"); 
        }
    });
});
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: