This is what I do:
function doOnOrientationChange() {
switch(window.orientation) {
case -90: case 90:
alert('landscape');
break;
default:
alert('portrait');
break;
}
}
window.addEventListener('orientationchange', doOnOrientationChange);
// Initial execution if needed
doOnOrientationChange();
Update May 2019: window.orientation
is a deprecated feature and not supported by most browsers according to MDN. The orientationchange
event is associated with window.orientation and therefore should probably not be used.