Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

screen.lockOrientation is not a function

I would like to use the API screen in Js with chrome.

 if ( navigator.userAgent.match( /(android|iphone)/gi ) ) {
        if ( 'orientation' in screen ) {
            //console.log( '// API supported, yeah!' );
            //console.log( 'new orientation is ', screen.orientation );
            screen.lockOrientation( 'landscape' );
        } else {
            console.log( '// API not supported ' );
        }
    } else {
        //alert('none');
    }

my error js : caught TypeError: screen.lockOrientation is not a function

/* other */

if ( navigator.userAgent.match( /(android|iphone)/gi ) ) {
    if ( 'orientation' in screen ) {
        let locOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation;
        locOrientation('landscape');
        console.log( '// API supported, yeah!' , locOrientation);
        console.log( 'new orientation is ', screen.orientation );
        //screen.lockOrientation( 'landscape' );
    } else {
        console.log( '// API not supported ' );
    }
} else {
    //alert('none');
}

my error js : locOrientation is not a function

update : 20/04/2017 -> With Javascript, we can't lock orientation with the navigator.

like image 551
Seb Avatar asked Mar 22 '17 15:03

Seb


People also ask

Why is my lock screen rotation not working?

If the Android screen rotation not working happens to you , or you're just not a fan of the feature, you can re-enable screen auto-rotate on your phone. Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on.

How do I lock screen rotation in react?

Screen orientation lock​ For Android, open the AndroidManifest. xml file and within the activity element add 'android:screenOrientation="portrait"' to lock to portrait or 'android:screenOrientation="landscape"' to lock to landscape.

How do I change the orientation of a Web page?

To change the orientation of the whole document, select Layout > Orientation. Choose Portrait or Landscape.

What is lock orientation?

If you don't want the screen to switch between portrait and landscape when you move the device, you can lock the screen orientation. To do this, swipe down from the right side of the top panel.


1 Answers

This is because lockOrientation is still prefixed. See the [2] footnote in this MDN article. Also, in Chrome it is under orientation.lock. See the [1] footnote in the MDN article.

locOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation || screen.orientation.lock;
locOrientation('landscape');

Please note that not all browsers have this function. Please check if locOrientation is set before you use it.

Edit: Adding the newer orientation.lock to the example.

like image 192
JosiahDaniels Avatar answered Oct 19 '22 02:10

JosiahDaniels