files/en-us/web/api/screenorientation/lock/index.md
{{APIRef("Screen Orientation")}}
The lock() method of the {{domxref("ScreenOrientation")}} interface locks the orientation of the containing document to the specified orientation.
Typically orientation locking is only enabled on mobile devices, and when the browser context is full screen. If locking is supported, then it must work for all the parameter values listed below.
lock(orientation)
orientation
"any"
portrait-primary, portrait-secondary, landscape-primary or landscape-secondary."natural"
portrait-primary or landscape-primary."landscape"
landscape-primary, landscape-secondary, or both."portrait"
portrait-primary, portrait-secondary, or both."portrait-primary"
portrait-primary and portrait-secondary; one of those will be assigned the angle of 90 degrees and the other will have an angle of 270 degrees."portrait-secondary"
portrait-primary."landscape-primary"
landscape-primary with an angle of either 90 or 270 degrees (landscape-secondary will be the other orientation and angle)."landscape-secondary"
landscape-primary.A {{jsxref("Promise")}} that resolves after locking succeeds.
The promise may be rejected with the following exceptions:
InvalidStateError {{domxref("DOMException")}}
SecurityError {{domxref("DOMException")}}
allow-orientation-lock of the sandbox attribute of the iframe element).NotSupportedError {{domxref("DOMException")}}
AbortError {{domxref("DOMException")}}
lock() method invoking or if {{domxref("ScreenOrientation/unlock","unlock()")}} is called while the lock promise is pending.This example shows how to lock the screen to the opposite orientation of the current one. Note that this example will only work on mobile devices and other devices that support orientation changes.
<div id="example_container">
<button id="fullscreen_button">Fullscreen</button>
<button id="lock_button">Lock</button>
<button id="unlock_button">Unlock</button>
<textarea id="log" rows="7" cols="85"></textarea>
</div>
const log = document.getElementById("log");
// Lock button: Lock the screen to the other orientation (rotated by 90 degrees)
const rotateBtn = document.querySelector("#lock_button");
rotateBtn.addEventListener("click", () => {
log.textContent += `Lock pressed \n`;
const oppositeOrientation = screen.orientation.type.startsWith("portrait")
? "landscape"
: "portrait";
screen.orientation
.lock(oppositeOrientation)
.then(() => {
log.textContent = `Locked to ${oppositeOrientation}\n`;
})
.catch((error) => {
log.textContent += `${error}\n`;
});
});
// Unlock button: Unlock the screen orientation (if locked)
const unlockBtn = document.querySelector("#unlock_button");
unlockBtn.addEventListener("click", () => {
log.textContent += "Unlock pressed \n";
screen.orientation.unlock();
});
// Full screen button: Set the example to fullscreen.
const fullscreenBtn = document.querySelector("#fullscreen_button");
fullscreenBtn.addEventListener("click", () => {
log.textContent += "Fullscreen pressed \n";
const container = document.querySelector("#example_container");
container.requestFullscreen().catch((error) => {
log.textContent += `${error}\n`;
});
});
To test the example, first press the Fullscreen button. Once the example is full screen, press the Lock button to switch the orientation, and Unlock to return to the natural orientation.
{{EmbedLiveSample('Examples')}}
{{Specifications}}
{{Compat}}