Transient user activation is required to enter fullscreen mode (the user has to interact with the page or a UI element in order for this feature to work).
Fullscreen mode is controlled by the Permissions-Policy directive fullscreen.
The default allowlist for fullscreen is self.
This allows fullscreen usage in same-origin nested frames but prevents it in third-party content.
Third-party usage can be enabled by the server first setting the Permissions-Policy header to grant permission to a particular third-party origin.
http
Permissions-Policy: fullscreen=(self "https://b.example.com")
Then the allow="fullscreen" attribute must be added to the frame container element for sources from that origin:
html
<iframe src="https://b.example.com" allow="fullscreen"></iframe>
The Permissions API fullscreen permission can be used to test whether access to use the mode is granted, denied or prompt (requires user acknowledgement of a prompt).
In this example, a video is presented in a web page. Pressing the Enter key lets the user toggle between windowed and fullscreen presentation of the video.
View Live Example
Watching for the Enter key
When the page is loaded, this code is run to set up an event listener to watch for the Enter key.
js
const video = document.getElementById("video");
// On pressing ENTER call toggleFullScreen method
document.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
toggleFullScreen(video);
}
});
Toggling fullscreen mode
This code is called by the event handler above when the user hits the Enter key.
js
function toggleFullScreen(video) {
if (!document.fullscreenElement) {
// If the document is not in full screen mode
// make the video full screen
video.requestFullscreen();
} else {
// Otherwise exit the full screen
document.exitFullscreen?.();
}
}
This starts by looking at the value of the document's fullscreenElement attribute. If the value is null, the document is currently in windowed mode, so we need to switch to fullscreen mode; otherwise, it's the element that's currently in fullscreen mode. Switching to fullscreen mode is done by calling Element.requestFullscreen() on the <video> element.
If fullscreen mode is already active (fullscreenElement is not null), we call exitFullscreen() on the document to shut off fullscreen mode.