Is there a way that works for all browsers?
Advertisement
Answer
original answer
Yes.
JavaScript
x
3
1
window.screen.availHeight
2
window.screen.availWidth
3
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
andwindow.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:
JavaScript131window.screen.availHeight
2window.screen.availWidth
3
For the absolute width/height, use:
JavaScript131window.screen.height
2window.screen.width
3