JavaScript Screen lockOrientation() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated
Non-standard
Instance method

What You’ll Learn

Screen.lockOrientation() is a deprecated, non-standard instance method that locked the screen into one or more orientations. Learn the string and array argument forms, the old vendor prefixes, when it could run (installed apps / fullscreen), and how to migrate to screen.orientation.lock()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Boolean

03

Status

Deprecated · Non-standard

04

Argument

String or string[]

05

Modern path

orientation.lock()

06

Context

App / fullscreen

Introduction

Games and video players sometimes want the device to stay in landscape. Older APIs tried to do that with screen.lockOrientation("landscape-primary") (or an array of allowed orientations).

That approach is obsolete. Modern browsers expose locking on the ScreenOrientation object from screen.orientation. This tutorial covers the legacy method so you can read old code and migrate it safely.

JavaScript
// Legacy only — do not use in new apps
// screen.lockOrientation("landscape-primary");
💡
Beginner tip

MDN: the old method only worked for installed Web apps or pages in fullscreen. A normal browser tab usually could not lock orientation.

Understanding the Method

MDN: the lockOrientation() method of the Screen interface locks the screen into a specified orientation.

  • Instance method — historically on window.screen.
  • Deprecated & Non-standard — not in any specification.
  • Argument — one orientation string, or an array of strings.
  • Returntrue authorized / false denied (not instant lock proof).

📝 Syntax

JavaScript
lockOrientation(orientation)

Parameters

orientation — a string or an array of strings. Passing several strings lets the screen rotate only among those orientations.

StringMeaning (legacy)
portrait-primaryPrimary portrait
portrait-secondarySecondary (flipped) portrait
landscape-primaryPrimary landscape
landscape-secondarySecondary (flipped) landscape
portraitBoth portrait modes
landscapeBoth landscape modes
defaultDevice natural primary orientation

Return value

A boolean: true if locking was authorized, false if denied. MDN: the return does not prove the screen is already locked—there may be a delay.

🎯 Prefer ScreenOrientation.lock()

LegacyModern
screen.lockOrientation(…)screen.orientation.lock(…)
Returns booleanReturns a Promise
Deprecated / non-standardScreen Orientation API
Unlock via unlockOrientation (legacy)screen.orientation.unlock()
JavaScript
// Modern idea (still needs fullscreen / policy in many browsers)
if (screen.orientation && screen.orientation.lock) {
  screen.orientation.lock("landscape").then(() => {
    console.log("locked (modern API)");
  }).catch((err) => {
    console.log("lock failed:", err.name);
  });
}

🛡️ Legacy Feature-Detection Pattern

MDN’s examples normalize prefixed names. Use this only when reading old code—not for new apps.

JavaScript
const lockOrientationUniversal =
  screen.lockOrientation ||
  screen.mozLockOrientation ||
  screen.msLockOrientation;

console.log(typeof lockOrientationUniversal);

⚡ Quick Reference

GoalCode / note
Legacy lock (string)screen.lockOrientation("landscape-primary")
Legacy lock (array)lockOrientation(["landscape-primary", …])
Detect prefixeslockOrientation || moz… || ms…
Modern replacementscreen.orientation.lock(…)
MDN statusDeprecated · Non-standard
New codeDo not use

🔍 At a Glance

Four facts about screen.lockOrientation().

Kind
method

Instance

Returns
boolean

Authorized?

Via
window.screen

When present

Status
deprecated

Non-standard

Examples Gallery

Examples follow MDN Screen.lockOrientation(). Labs feature-detect and usually do not lock your display—they report API availability.

📚 Getting Started

Detect the legacy API and study MDN’s call shapes without forcing a lock.

Example 1 — Feature-Detect Prefixed Names

Find which lock function (if any) exists on screen.

JavaScript
const lockOrientationUniversal =
  screen.lockOrientation ||
  screen.mozLockOrientation ||
  screen.msLockOrientation;

if (typeof lockOrientationUniversal === "function") {
  console.log("legacy lockOrientation API present");
} else {
  console.log("lockOrientation not supported (expected on modern browsers)");
}
Try It Yourself

How It Works

Matches MDN’s universal alias. Missing support is the normal modern outcome.

Example 2 — MDN String Argument (Safe Demo)

Show the string call shape; only invoke if the API exists.

JavaScript
const lockOrientationUniversal =
  screen.lockOrientation ||
  screen.mozLockOrientation ||
  screen.msLockOrientation;

if (typeof lockOrientationUniversal !== "function") {
  console.log("API missing; would have called lockOrientation(\"landscape-primary\")");
} else {
  const ok = lockOrientationUniversal("landscape-primary");
  console.log(ok ? "Orientation was locked (authorized)" : "Orientation lock failed");
}
Try It Yourself

How It Works

From MDN’s string example. On supporting engines, true/false means authorized vs denied—not always an instant visual lock.

📈 Arrays, Modern API & Snapshots

Compare the array form with today’s ScreenOrientation.lock().

Example 3 — MDN Array Argument (Safe Demo)

Allow both landscape orientations in the legacy API.

JavaScript
const lockOrientationUniversal =
  screen.lockOrientation ||
  screen.mozLockOrientation ||
  screen.msLockOrientation;

const orientations = ["landscape-primary", "landscape-secondary"];

if (typeof lockOrientationUniversal !== "function") {
  console.log("API missing; would lock to:", orientations.join(", "));
} else {
  const ok = lockOrientationUniversal(orientations);
  console.log(ok ? "Orientation was locked" : "Orientation lock failed");
}
Try It Yourself

How It Works

MDN: with multiple locks, the screen may rotate among the listed orientations until unlocked.

Example 4 — Modern orientation.lock Check

Detect the replacement without requiring a successful lock.

JavaScript
const hasModern =
  !!(screen.orientation && typeof screen.orientation.lock === "function");

console.log({
  hasModernLock: hasModern,
  currentType: screen.orientation ? screen.orientation.type : "(none)",
  tip: "Prefer screen.orientation.lock() in new apps (fullscreen/policy may apply)."
});
Try It Yourself

How It Works

Even when lock exists, browsers often reject the promise outside fullscreen or without the right permissions—always handle rejection.

Example 5 — Legacy vs Modern API Snapshot

One object summarizing what this browser exposes.

JavaScript
console.log({
  lockOrientation: typeof screen.lockOrientation,
  mozLockOrientation: typeof screen.mozLockOrientation,
  msLockOrientation: typeof screen.msLockOrientation,
  orientationLock: screen.orientation
    ? typeof screen.orientation.lock
    : "no screen.orientation",
  status: "legacy lockOrientation is deprecated & non-standard"
});
Try It Yourself

How It Works

Use this dump when auditing a codebase for leftover lockOrientation / mozLockOrientation calls.

🚀 Common Use Cases

  • Reading / migrating old fullscreen games that called lockOrientation.
  • Understanding vendor-prefixed orientation lock history.
  • Teaching why orientation lock is restricted to apps / fullscreen.
  • Auditing projects for deprecated Screen methods to remove.
  • Planning a move to screen.orientation.lock() with Promise error handling.

🔧 How It Works

1

App requests a fixed orientation

Often landscape for games or video.

Intent
2

Legacy Screen.lockOrientation

Boolean authorize/deny in installed apps or fullscreen.

Legacy
3

Modern ScreenOrientation.lock

Promise-based API on screen.orientation.

Today
4

Always handle failure

Many environments deny locks; keep a rotatable fallback UI.

📝 Notes

Legacy / Deprecated Support

Screen.lockOrientation() is Deprecated and Non-standard. Prefer screen.orientation.lock(). Logos use the shared browser-image-sprite.png sprite from this project. Do not rely on the legacy method for production.

Deprecated · Non-standard

Screen.lockOrientation()

Legacy orientation lock — use ScreenOrientation.lock() instead.

Legacy Avoid in new apps
Mozilla Firefox Legacy mozLockOrientation era — prefer orientation.lock()
Avoid
Google Chrome Use ScreenOrientation.lock() where available
Avoid legacy
Microsoft Edge Legacy msLockOrientation history — prefer modern API
Avoid legacy
Apple Safari Do not rely on screen.lockOrientation()
Unavailable / Avoid
Opera Follow Chromium Screen Orientation API
Avoid legacy
Internet Explorer msLockOrientation legacy only
Legacy
lockOrientation() Deprecated

Bottom line: Feature-detect only for migration. Prefer screen.orientation.lock() with Promise error handling; never require a successful lock for core UX.

Conclusion

screen.lockOrientation() was a deprecated, non-standard way to lock screen orientation with a string or array argument. Feature-detect it only when migrating legacy code, and use screen.orientation.lock() for new work—always with a fallback when the lock is denied.

Continue with unlockOrientation(), orientation, Window methods, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Prefer screen.orientation.lock() for new apps
  • Handle Promise rejections when locking fails
  • Keep a UI that works when orientation cannot be locked
  • Remove lockOrientation / mozLockOrientation from codebases
  • Read orientation for type/angle

❌ Don’t

  • Use screen.lockOrientation() in new production apps
  • Assume a true return means an instant lock
  • Require orientation lock for core product flows
  • Ignore fullscreen / installed-app constraints from MDN
  • Skip checking MDN’s Deprecated and Non-standard warnings

Key Takeaways

Knowledge Unlocked

Five things to remember about lockOrientation()

Deprecated non-standard orientation lock method.

5
Core concepts
⚠️ 02

Deprecated

avoid in new code

Status
🚫 03

Non-standard

no public spec

Spec
🔄 04

Modern

orientation.lock()

Replace
🎯 05

Fallback

locks often fail

UX

❓ Frequently Asked Questions

It was a deprecated, non-standard method that locked the screen into a specified orientation (string or array of strings). Prefer ScreenOrientation.lock() instead.
MDN marks Screen.lockOrientation() as Deprecated and Non-standard. It is not Experimental. Do not use it in new code.
Use screen.orientation.lock(orientation) from the Screen Orientation API. It returns a Promise and is the modern path MDN recommends.
MDN: only for installed Web apps or for Web pages in fullscreen mode. Ordinary tabs usually could not lock orientation.
true if locking was authorized, false if denied. MDN notes this does not guarantee the screen is already locked—there may be a delay.
Vendors shipped prefixed names (mozLockOrientation, msLockOrientation). Legacy snippets often feature-detect across those names.
Did you know?

Locking orientation without user context would be surprising on a phone. That is why both the old and new APIs are gated (installed app, fullscreen, or similar policies)—and why a denied lock is a normal result, not necessarily a bug in your code.

Next: Screen.unlockOrientation()

Learn the deprecated non-standard orientation unlock method.

unlockOrientation() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful