How can I get windowWidth
, windowHeight
, pageWidth
, pageHeight
, screenWidth
, screenHeight
, pageX
, pageY
, screenX
, screenY
which will work in all major browsers?
|
|||||||||||||||||||||
|
If you are using jQuery, you can get the size of the window or the document using jQuery methods:
For screen size you can use the
|
|||||||||||||||||||||
|
|
|||
|
This is everything you need to know:
but in short:
|
|||||||||||||||||
|
A non-jQuery way to get the available screen dimension.
http://www.quirksmode.org/dom/w3c_cssom.html#t10 :
|
||||
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:
In most cases it will be the best way, in example, to display a perfectly centred floating modal dialog. It allows you to calculate positions on window, no matter which resolution orientation or window size is using the browser. |
|||||
|
Here is a cross browser solution with pure JavaScript (Source):
|
|||||||||
|
Sometimes you need to see the width/height changes while resizing the window and inner content. For that I've written a little script that adds a log box that dynamicly monitors all the resizing and almost immediatly updates. It adds a valid HTML with fixed position and high z-index, but is small enough, so you can:
|
|||
|
If you need a truly bulletproof solution for the document width and height (the It has just one purpose: to always return the correct document size, even in scenarios when jQuery and other methods fail. Despite its name, you don't necessarily have to use jQuery – it is written in vanilla Javascript and works without jQuery, too. Usage:
for the global
Update: now for window dimensions, too Ever since version 1.1.0, jQuery.documentSize also handles window dimensions. That is necessary because
jQuery.documentSize provides |
||||
|
But when we talk about responsive screens and if we want to handle it using jQuery for some reason,
gives the correct measurement. Even it removes the scroll-bar's extra space and we don't need to worry about adjusting that space :) |
|||||||||
|
In some cases related with responsive layout
That represents actual size of wrapper. |
|||
|
You can use the The following is an example of what it would return:
To get your To get your window width and height, it would be For the To get the width and height of your page, use jQuery's Again using jQuery, use |
|||
|