Alex Rivera | Logout

Detect rotation of Android phone in the browser with JavaScript

Asked 2009-10-30T10:43:20.077
196

I know that in Safari on an iPhone you can detect the screen's orientation and change of orientation by listening for the onorientationchange event and querying window.orientation for the angle.

Is this possible in the browser on Android phones?

To be clear, I am asking whether the rotation of an Android device can be detected by JavaScript running on a standard web page. It is possible on an iPhone, and I wondered whether it could be done for Android phones.

Edit
Report

1 Answer

12

You could always listen to the window resize event. If, on that event, the window went from being taller than it is wide to wider than it is tall (or vice versa), you can be pretty sure the phone orientation was just changed.

answered 2009-10-30T17:21:49.180

Your Answer