How can I get windowWidth
, windowHeight
, pageWidth
, pageHeight
, screenWidth
, screenHeight
,
If you need a truly bulletproof solution for the document width and height (the pageWidth
and pageHeight
in the picture), you might want to consider using a plugin of mine, jQuery.documentSize.
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:
var w = $.documentWidth(),
h = $.documentHeight();
for the global document
. For other documents, e.g. in an embedded iframe you have access to, pass the document as a parameter:
var w = $.documentWidth( myIframe.contentDocument ),
h = $.documentHeight( myIframe.contentDocument );
Update: now for window dimensions, too
Ever since version 1.1.0, jQuery.documentSize also handles window dimensions.
That is necessary because
$( window ).height()
is buggy in iOS, to the point of being useless$( window ).width()
and $( window ).height()
are unreliable on mobile because they don't handle the effects of mobile zooming. jQuery.documentSize provides $.windowWidth()
and $.windowHeight()
, which solve these issues. For more, please check out the documentation.