Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get browser width and height after user resizes the window

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?

like image 570
Gregwest85 Avatar asked Apr 05 '13 17:04

Gregwest85


People also ask

How do I find my browser width and height?

Use window. innerWidth and window. innerHeight to get the current screen size of a page.

How do I capture a Windows resize event?

Answer: Use the addEventListener() Method You can simply use the addEventListener() method to register an event handler to listen for the browser window resize event, such as window. addEventListener('resize', ...) . The following example will display the current width and height of the browser window on resize.

What method is used to get the size of a browser window?

You can also get the WINDOW width and height, avoiding browser toolbars and other stuff. It is the real usable area in browser's window. To do this, use: window. innerWidth and window.

How do I stop a browser window from resizing after a specific size?

You can not control the size of the window. You can use CSS to set min-width and min-height properties to ensure your page layout stays sane. using 'window. open' I can open a page with a fixed height width.


8 Answers

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 :)

like image 196
Pablo Mescher Avatar answered Oct 21 '22 09:10

Pablo Mescher


It's possible by listening to resize event.

$(window).resize(function() {
  var width = $(window).width();
  var height = $(window).height();
})
like image 45
keune Avatar answered Oct 21 '22 09:10

keune


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...
  });
like image 21
Jay Bhatt Avatar answered Oct 21 '22 09:10

Jay Bhatt


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.

like image 23
Honza Kalfus Avatar answered Oct 21 '22 09:10

Honza Kalfus


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);
    }
like image 23
Ikrom Avatar answered Oct 21 '22 08:10

Ikrom


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

like image 21
What have you tried Avatar answered Oct 21 '22 07:10

What have you tried


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();

});
like image 33
Anoop Avatar answered Oct 21 '22 09:10

Anoop


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>  
like image 21
Abdulmajeed Naji Alshari Avatar answered Oct 21 '22 09:10

Abdulmajeed Naji Alshari