Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
80
rated 0 times [  84] [ 4]  / answers: 1 / hits: 169100  / 15 Years ago, fri, october 30, 2009, 12:00:00

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.


More From » android

 Answers
6

To detect an orientation change on an Android browser, attach a listener to the orientationchange or resize event on window:



// Detect whether device supports orientationchange event, otherwise fall back to
// the resize event.
var supportsOrientationChange = onorientationchange in window,
orientationEvent = supportsOrientationChange ? orientationchange : resize;

window.addEventListener(orientationEvent, function() {
alert('HOLY ROTATING SCREENS BATMAN:' + window.orientation + + screen.width);
}, false);


Check the window.orientation property to figure out which way the device is oriented. With Android phones, screen.width or screen.height also updates as the device is rotated. (this is not the case with the iPhone).


[#98413] Tuesday, October 27, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keric

Total Points: 572
Total Questions: 93
Total Answers: 97

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
;