Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect window size with jQuery?

How can I detect window/browser size with jQuery like Gmail? In Gmail, we don't need to refresh or reload current page as soon as we have changed window resolution in window setting? In my project, I need to refresh the browser as soon as window setting has been changed.

Any idea will be appreciated.

like image 447
PPShein Avatar asked Oct 17 '11 03:10

PPShein


People also ask

How do I determine window size?

To find the width, measure jamb-to-jamb from the inside of the trim on one side of the window to the inside of the trim on the other side. Measure at the bottom, middle and top of the window. Record the shortest measurement as the width.

How does jQuery calculate window height?

Basically, $(window). height() give you the maximum height inside of the browser window (viewport), and $(document). height() gives you the height of the document inside of the browser. Most of the time, they will be exactly the same, even with scrollbars.

What is the meaning of $( window width ()?

$(window). width() gets the entire width of the window, including things like the scroll bar .

How do I set the width of an element in jQuery?

jQuery width() Method The width() method sets or returns the width of the selected elements. When this method is used to return width, it returns the width of the FIRST matched element. When this method is used to set width, it sets the width of ALL matched elements.


1 Answers

You cannot really find the display resolution from a web page. There is a CSS Media Queries statement for it, but it is poorly implemented in most devices and browsers, if at all. However, you do not need to know the resolution of the display, because changing it causes the (pixel) width of the window to change, which can be detected using the methods others have described:

$(window).resize(function() {   // This will execute whenever the window is resized   $(window).height(); // New height   $(window).width(); // New width }); 

You can also use CSS Media Queries in browsers that support them to adapt your page's style to various display widths, but you should really be using em units and percentages and min-width and max-width in your CSS if you want a proper flexible layout. Gmail probably uses a combination of all these.

like image 110
Félix Saparelli Avatar answered Sep 21 '22 15:09

Félix Saparelli