Two ways to do this:
Firstly, according to the Screen API documentation, using> = Chrome 38, Firefox, and IE 11, the display object is available not only to view the orientation, but also to register a listener each time the device orientation changes.
screen.orientation.type will explicitly tell you what orientation is, and for the listener you can use something simple:
screen.orientation.onchange = function (){ // logs 'portrait' or 'landscape' console.log(screen.orientation.type.match(/\w+/)[0]); };
Secondly, for compatibility with other browsers, such as Safari, which are not compatible with Screen, this post shows that you can continue to use innerWidth and innerHeight when resizing the window.
function getOrientation(){ var orientation = window.innerWidth > window.innerHeight ? "Landscape" : "Portrait"; return orientation; } window.onresize = function(){ getOrientation(); }
source share