How to detect the screen resolution with JavaScript?

iceorangeblue picture iceorangeblue · Feb 11, 2010 · Viewed 367.9k times · Source

Is there a way that works for all browsers?

Answer

John Weldon picture John Weldon · Feb 11, 2010

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