Is there a way to detect if the browser/device supports screen rotation, without having to wait for the orientationchange event? I'm not trying to detect the rotation itself, only if the browser or device supports it.
Asked
Active
Viewed 1,527 times
5
-
possible duplicate of [**Detect rotation of Android phone in the browser with javascript**](http://stackoverflow.com/questions/1649086/detect-rotation-of-android-phone-in-the-browser-with-javascript) – Nope Apr 12 '13 at 11:29
2 Answers
5
I think you can detect it like so:
if( 'onorientationchange' in window) { /* supported! */ }
However, I'm not sure if some browsers will support the event even though they never fire it.

Niet the Dark Absol
- 320,036
- 81
- 464
- 592
-
For thoroughness it might be worth checking to see if it holds a value for `window.orientation` too, but if the browser lies about supporting/firing `onorientationchange` there's a good chance it'll present a complete 'lie' I guess. – David Thomas Apr 12 '13 at 11:28
-
Thank you both for the answers. I guess this is the best possible solution, but it won't be triggered by some devices (according to this page: http://davidwalsh.name/orientation-change)... – jvilhena Apr 12 '13 at 11:51
0
You should assume that all devices can change their orientation because you can turn any screen to the other orientation using the OS. This is unless you lock the orientation, which is not recommended in most cases.
Technically, window.orientation
and window.orientationchange
are deprecated. We should instead use the screen.orientation
object and change
event listener.
However, Safari does not yet support screen orientation. So you should have both for now:
if (screen.orientation) {
screen.orientation.addEventListener('change', handleOrientationChange);
} else {
window.addEventListener('orientationchange', handleOrientationChange);
}
When the device is not rotated this listener will not fire. But it could fire for any device.
For more information, see "Managing screen orientation" in MDN's docs.

Zach Saucier
- 24,871
- 12
- 85
- 147