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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.