24

Is there any way to get the browser width and height after a user has resized the window. For example if the window is 1920 by 1080 and the user changes the window to 500 by 500 is there any way to get those two new values in JavaScript or jquery?

Gregwest85
  • 291
  • 2
  • 4
  • 8

9 Answers9

32

Pure Javascript answer:

var onresize = function() {
   //your code here
   //this is just an example
   width = document.body.clientWidth;
   height = document.body.clientHeight;
}
window.addEventListener("resize", onresize);

This works fine on chrome. However, it works only on chrome. A slightly more cross-browser example is using the event target properties "outerWidth" and "outerHeight", since in this case the event "target" is the window itself. The code would be like this

var onresize = function(e) {
   //note i need to pass the event as an argument to the function
   width = e.target.outerWidth;
   height = e.target.outerHeight;
}
window.addEventListener("resize", onresize);

This works fine in firefox and chrome

Hope it helps :)

Edit: Tested in ie9 and this worked too :)

Pablo Mescher
  • 26,057
  • 6
  • 30
  • 33
  • Edit: document.width and document.height should not be used anymore and did not work for me on Chrome. Instead it is recommended to use document.body.clientWidth and document.body.clientHeight. See http://www.howtocreate.co.uk/tutorials/javascript/browserwindow – Daniel Wallman Mar 09 '17 at 19:55
  • Edit: You need to update the target to `e.currentTarget.outerWidth` instead of `e.target.outerWidth`. – jaguarj Dec 02 '22 at 04:06
12

If you need to know these values to do layout adjustments, I bet you plan on listening to those values. I recommended using the Window.matchmedia() API for that purpose instead.

It is much more performant and is basically the JS equivalent of CSS media queries.

Very quick example of use:

if (window.matchMedia("(max-width: 500px)").matches) {
  /* the viewport is less than or exactly 500 pixels wide */
} else {
  /* the viewport is more than 500 pixels wide */
}

You can also setup a listener that'll get called every time the state of the matches property changes.

See MDN for description and example of using a listener.

Jan Kalfus
  • 5,422
  • 2
  • 30
  • 38
  • this doesn't work like css media in that it doesn't listen....put a console.log into that function and you'll see that it doesn't fire when the window changes size. it only works on the page load. – luke_mclachlan May 18 '21 at 10:10
  • @luke_mclachlan It seems like you haven't read the whole answer ;) – Jan Kalfus May 18 '21 at 14:18
5

It's possible by listening to resize event.

$(window).resize(function() {
  var width = $(window).width();
  var height = $(window).height();
})
keune
  • 5,779
  • 4
  • 34
  • 50
5

You can use the JQuery resize() function. Also make sure you add the same resize logic to reload event. If user reloads in the sized window your logic won't work.

 $(window).resize(function() {
                        $windowWidth = $(window).width();
                            $windowHeight = $(window).height();
                        });

 $(document).ready(function() {
      //same logic that you use in the resize...
  });
Jay Bhatt
  • 5,601
  • 5
  • 40
  • 62
  • Jay. thanks. Is it possible to avoid code duplication? I use mega-menu for my site and need window auto resize on window size changes and after reloading. At this time I use the same code on `resize()` and `ready()` events. – Andry Aug 24 '14 at 07:43
4

Practically, I use this and it helps me a lot:

    var TO = false;
    var resizeEvent = 'onorientationchange' in window ? 'orientationchange' : 'resize';
    $(window).bind(resizeEvent, function() {
        TO && clearTimeout(TO);
        TO = setTimeout(resizeBody, 200);
    });
    function resizeBody(){
        var height = window.innerHeight || $(window).height();
        var width = window.innerWidth || $(window).width();
        alert(height);
        alert(width);
    }
Ikrom
  • 4,785
  • 5
  • 52
  • 76
1

Use jQuery resize method to listen window size change . inside callback you can get height and width.

$(window).resize(function(){
    var width = $(window).width();
    var height = $(window).height();

});
Anoop
  • 23,044
  • 10
  • 62
  • 76
1

You can use the resize event, along with the height() and width() properties

$(window).resize(function(){
   var height = $(window).height();
   var width = $(window).width();
});

See some more examples here

What have you tried
  • 11,018
  • 4
  • 31
  • 45
1

Simplest way to get real width and height of an element after window resize as the follow:

<div id="myContainer">
    <!--Some Tages ...  -->
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(function () {
        $(window).resize(function () {
            //The below two lines of codes more Important to clear the previous settings to get the current measure of width and height
            $('#myContainer').css('height', 'unset');
            $('#myContainer').css('width', 'unset');

            var element = $('#myContainer');

            var height = element.height();
            var width = element.width();

            //Below two lines will includes padding but not border 
            var innerHeight = element.innerHeight();
            var innerWidth = element.innerWidth();

            //Below two lines will includes padding, border but no margin 
            var outerHeight = element.outerHeight();
            var outerWidth = element.outerWidth();

            //Below two lines will includes padding, border and margin 
            var outerHeight = element.outerHeight(true);
            var outerWidth = element.outerWidth(true);
        });
    });
</script>  
1

You can use the event object to get the height and width, I use destructuring assignment and the target points to window:

const handleGetDim = ({ target }) => ({
  width: target.innerWidth,
  height: target.innerHeight,
});

window.addEventListener('resize', handleGetDim);
AmerllicA
  • 29,059
  • 15
  • 130
  • 154