Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect device orientation with JavaScript?

I've seen a number of ways to detect the screen orientation of the device, including using a check to test innerWidth versus innerHeight, like so.

function getOrientation(){
    var orientation = window.innerWidth > window.innerHeight ? "Landscape" : "Primary";
    return orientation;
}

But what if I want to check for changes in screen orientation as well with an event listener? I've tried this code but it doesn't work for me.

  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();

It doesn't detect device orientation, and the listener does not register for me (I'm using Chrome's device emulator).

like image 636
Fahd Sheikh Avatar asked Apr 15 '16 15:04

Fahd Sheikh


People also ask

How can we detect the orientation of the screen in Android?

int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.

What Javascript event can you use to respond to data collected by a computer device's gyroscope?

The first one is the DeviceOrientationEvent , which is sent when the accelerometer detects a change to the orientation of the device. By receiving and processing the data reported by these orientation events, it's possible to interactively respond to rotation and elevation changes caused by the user moving the device.

How do I force Javascript to landscape?

screen. orientation. lock('landscape'); Will force it to change to and stay in landscape mode.

Can I use window orientation?

orientation. Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes.


1 Answers

There are two ways to do this:

First, as per the Screen API documentation, using >= Chrome 38, Firefox, and IE 11, the screen object is available to not only view the orientation, but to also register the listener on each time the device orientation changes.

screen.orientation.type will explicitly let you know what the orientation is, and for the listener you can use something simple like:

screen.orientation.onchange = function (){
    // logs 'portrait' or 'landscape'
    console.log(screen.orientation.type.match(/\w+/)[0]);
};

Second, for compatibility with other browsers like Safari that aren't compatible with Screen, this post shows that you can continue to use innerWidth and innerHeight on window resizing.

 function getOrientation(){
    var orientation = window.innerWidth > window.innerHeight ? "Landscape" : "Portrait";
    return orientation;
}

 window.onresize = function(){ getOrientation(); }
like image 96
Fahd Sheikh Avatar answered Sep 24 '22 14:09

Fahd Sheikh