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.
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.
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.
To change the orientation of the whole document, select Layout > Orientation. Choose Portrait or Landscape.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With