Alex Rivera | Logout

How to detect the screen resolution with JavaScript?

Asked 2010-02-11T03:36:29.530
272

Is there a way that works for all browsers?

Edit
Report

3 Answers

413

original answer

Yes.

window.screen.availHeight
window.screen.availWidth

update 2017-11-10

From Tsunamis in the comments:

To get the native resolution of i.e. a mobile device you have to multiply with the device pixel ratio: window.screen.width * window.devicePixelRatio and window.screen.height * window.devicePixelRatio. This will also work on desktops, which will have a ratio of 1.

And from Ben in another answer:

In vanilla JavaScript, this will give you the AVAILABLE width/height:

window.screen.availHeight
window.screen.availWidth

For the absolute width/height, use:

window.screen.height
window.screen.width
answered 2010-02-11T03:39:27.140
59
var width = screen.width;
var height = screen.height;
answered 2012-10-11T19:19:01.367
7
answered 2010-02-11T03:40:25.773

Your Answer