document.exitPictureInPicture() is an instance method that asks the browser to leave Picture-in-Picture mode (see MDN Document: exitPictureInPicture()). Learn its Promise return, the InvalidStateError when nothing is in PiP, how it pairs with HTMLVideoElement.requestPictureInPicture() and pictureInPictureElement, and five try-it labs.
01
Kind
Instance method
02
Params
None
03
Returns
Promise
04
Reverses
requestPictureInPicture
05
Guard
pictureInPictureElement
06
Status
Limited availability
Fundamentals
Introduction
Picture-in-Picture (PiP) floats a video in a small window so users can keep watching while browsing other tabs or apps. You enter PiP with video.requestPictureInPicture(). To leave from script, call document.exitPictureInPicture().
MDN: this method requests that a video currently floating in PiP be taken out of that mode, restoring the previous screen state. It usually reverses a previous requestPictureInPicture() call.
💡
Think: exit door for the floating video
1) Check document.pictureInPictureElement 2) If set, call document.exitPictureInPicture() 3) Await the Promise (or use .then() / .catch()) 4) Sync UI with leavepictureinpicture / property checks
MDN: to track which video is in PiP, listen for enterpictureinpicture / leavepictureinpicture on the video, or compare document.pictureInPictureElement to your HTMLVideoElement.
Compare
⚖️ PiP vs fullscreen exit
Goal
Picture-in-Picture
Fullscreen
Enter
video.requestPictureInPicture()
el.requestFullscreen()
Exit
document.exitPictureInPicture()
document.exitFullscreen()
Status
document.pictureInPictureElement
document.fullscreenElement
Enabled?
document.pictureInPictureEnabled
document.fullscreenEnabled
Events
enterpictureinpicture / leavepictureinpicture
fullscreenchange
Cheat Sheet
⚡ Quick Reference
Goal
Code
Exit PiP
await document.exitPictureInPicture()
Safe exit
if (document.pictureInPictureElement) { await document.exitPictureInPicture(); }
Examples follow MDN Document: exitPictureInPicture() and practical PiP patterns. Entering PiP needs a real video and often a user gesture; support varies by browser.
📚 Getting Started
Toggle PiP like MDN’s sample.
Example 1 — MDN: click to toggle PiP
If already in PiP, exit; otherwise request PiP on a video.
JavaScript
const video = document.querySelector("#player");
document.onclick = () => {
if (document.pictureInPictureElement) {
document
.exitPictureInPicture()
.then(() => console.log("Document Exited from Picture-in-Picture mode"))
.catch((err) => console.error(err));
} else {
video.requestPictureInPicture();
}
};
Document.exitPictureInPicture() is marked Limited availability on MDN (not Baseline). Feature-detect with pictureInPictureEnabled before production use. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Limited availability · Not Baseline
Document.exitPictureInPicture()
Leave Picture-in-Picture with a Promise — pair with requestPictureInPicture and pictureInPictureElement.
Bottom line: Guard with pictureInPictureElement before exit. Handle the Promise, listen for leave events, and feature-detect for Limited availability.
Wrap Up
Conclusion
document.exitPictureInPicture() closes the floating PiP window from script: no parameters, a Promise, and an InvalidStateError if nothing is in PiP. Guard with pictureInPictureElement, catch rejections, and keep UI honest with leave events.
Check document.pictureInPictureElement before exiting
Handle the Promise with await + try/catch or .catch()
Listen for leavepictureinpicture to update buttons and labels
Feature-detect and check pictureInPictureEnabled
Enter PiP from a user gesture via video.requestPictureInPicture()
❌ Don’t
Call exit when nothing is in PiP (triggers InvalidStateError)
Assume Baseline support in every browser yet (MDN: Limited availability)
Confuse PiP with fullscreen APIs
Forget users can close PiP with browser UI
Ignore promise rejections when exit fails
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about exitPictureInPicture()
Leave PiP with a Promise; guard when nothing is floating.
5
Core concepts
📝01
Returns
Promise
MDN
🔄02
Params
none
MDN
⚠️03
If null
InvalidStateError
MDN
⚡04
Pair
requestPictureInPicture
video
🛡05
Status
Limited
not Baseline
❓ Frequently Asked Questions
MDN: Document.exitPictureInPicture() requests that a video in this document currently floating in picture-in-picture mode be taken out of PiP, restoring the previous screen state. It usually reverses HTMLVideoElement.requestPictureInPicture().
No. MDN does not mark Document.exitPictureInPicture() as Deprecated, Experimental, or Non-standard. It is Limited availability (not Baseline), so feature-detect in production.
A Promise that resolves when the user agent has finished exiting picture-in-picture mode. On failure, handle the rejection with catch() (MDN).
MDN: an InvalidStateError is thrown if document.pictureInPictureElement is null. Always check pictureInPictureElement before calling exitPictureInPicture().
Call video.requestPictureInPicture() on an HTMLVideoElement (usually from a user gesture). Check document.pictureInPictureEnabled first.
MDN: listen for enterpictureinpicture and leavepictureinpicture on the video element(s), or compare document.pictureInPictureElement to your video.
Did you know?
Like fullscreen, you enter on an element (HTMLVideoElement.requestPictureInPicture()) but exit on the Document. That is why status lives on document.pictureInPictureElement — one place to ask which video is floating.