Categories
JavaScript Answers

How to block device rotation on mobile web pages with JavaScript?

Spread the love

Sometimes, we want to block device rotation on mobile web pages with JavaScript.

In this article, we’ll look at how to block device rotation on mobile web pages with JavaScript.

How to block device rotation on mobile web pages with JavaScript?

To block device rotation on mobile web pages with JavaScript, we call the screen.orientation.lock or screen.lockOrientation method.

For instance, we write

screen.orientation.lock();

to lock the screen to the current orientation.

Or we write

screen.lockOrientation("portrait-primary");

to call screen.lockOrientation to lock the screen to the normal portrait orientation.

We can also call it with 'portrait-secondary' to lock it in the upside down portrait mode.

'landscape-primary' locks it to the normal landscape mode.

'landscape-secondary' locks it to the upside down landscape mode.

Conclusion

To block device rotation on mobile web pages with JavaScript, we call the screen.orientation.lock or screen.lockOrientation method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *