Search code examples
javascriptiossafariuiinterfaceorientation

Detecting initial device orientation on iOS in Safari


I know how to detect device orientation change on iOS in Safari, but I cannot fathom out how to detect initial orientation when my page loads.

It is simple to detect if the device is in portrait or landscape mode - just divide window width with window height and compare result with 1. But I don't see a way to tell portrait from flipped and landscape left from landscape right. Is there a way to do it?

(Why do I need it? Because accelerometer data doesn't flip together with screen when a device is turned on its head. The code must therefore know the current orientation to correctly process accelerometer data.)


Solution

  • You can check window.orientation on page load.