Although the question refers to only PhoneGap and iOS usage, and although it was already answered, I can add a few points to the broader question of detecting screen orientation with JS in 2019:
window.orientation
property is deprecated and not supported by Android browsers.There is a newer property that provides more information about the orientation - screen.orientation
. But it is still experimental and not supported by iOS Safari. So to achieve the best result you probably need to use the combination of the two: const angle = screen.orientation ? screen.orientation.angle : window.orientation
.
As @benallansmith mentioned in his comment, window.onorientationchange
event is fired before window.onresize
, so you won't get the actual dimensions of the screen unless you add some delay after the orientationchange event.
There is a Cordova Screen Orientation Plugin for supporting older mobile browsers, but I believe there is no need in using it nowadays.
There was also a screen.onorientationchange
event, but it is deprecated and should not be used. Added just for completeness of the answer.
In my use-case, I didn't care much about the actual orientation, but rather about the actual width and height of the window, which obviously changes with orientation. So I used resize
event to avoid dealing with delays between orientationchange
event and actualizing window dimensions:
window.addEventListener('resize', () => {
console.log(`Actual dimensions: ${window.innerWidth}x${window.innerHeight}`);
console.log(`Actual orientation: ${screen.orientation ? screen.orientation.angle : window.orientation}`);
});
Note 1: I used EcmaScript 6 syntax here, make sure to compile it to ES5 if needed.
Note 2: window.onresize
event is also fired when virtual keyboard is toggled, not only when orientation changes.