requestFullscreen Targets One Element At A Time
Request fullscreen on one element with requestFullscreen, track it with document.fullscreenElement, and listen to fullscreenchange for Esc exits.
Code
// Fullscreen is element-scoped: request it on the element, observe
// document.fullscreenElement, and exit with document.exitFullscreen()
const stage = document.querySelector('#stage');
const button = document.querySelector('#fullscreen');
button.addEventListener('click', async () => {
if (document.fullscreenElement) {
await document.exitFullscreen();
return;
}
// requestFullscreen resolves once the element really fills the screen
try {
await stage.requestFullscreen();
} catch (error) {
console.log('fullscreen denied:', error.name);
return;
}
console.log('fullscreen:', document.fullscreenElement === stage);
});
// fullscreenchange fires for every enter and exit, Esc included
document.addEventListener('fullscreenchange', () => {
console.log('fullscreenchange:', Boolean(document.fullscreenElement));
});How the snippet works
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.
Top-level bindings stage, button hold the demo state — nothing sits inside a function scope.
Caveats
- The snippet leans on
document— browser-only surfaces a bare Node process cannot provide.
Using it
The prefixes fullscreen denied:, fullscreen:, fullscreenchange: mark the run’s output; reading one tells you which statement ran.
Key statements
document.addEventListener('fullscreenchange', () => {
console.log('fullscreenchange:', Boolean(document.fullscreenElement));
});
await document.exitFullscreen();
await stage.requestFullscreen();
Interactive Tools for This Topic
Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.ISO 8601 Duration ParserParse ISO 8601 durations like PT1H30M into year, month, week, day, and time components with exact or approximate total seconds.
FAQ
- Does requestFullscreen Targets One Element At A Time run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.