When someone interacts with a webpage, the browser sends events to the relevant JavaScript code. The fullscreenerror event happens when an attempt to enter fullscreen fails.
When fullscreenerror happens
An attempt to enter fullscreen fails.
You can listen for it using addEventListener(). That keeps event handling separate from HTML attributes such as onclick and works well with modern code organization.
A small working example
<button id="demo-target" type="button">Fullscreen target</button>
<p id="event-status" role="status">Waiting for fullscreenerror...</p>
<script>
const eventTarget = document;
const eventStatus = document.getElementById("event-status");
if (eventTarget) {
eventTarget.addEventListener("fullscreenerror", (eventDetails) => {
eventStatus.textContent = "fullscreenerror event received";
console.log(eventDetails.type, eventDetails.target);
});
}
document.getElementById("demo-target").addEventListener("click", async () => {
try {
await document.documentElement.requestFullscreen();
} catch (fullscreenError) {
console.log(fullscreenError);
}
});
</script>
Reading the code
The browser listens for fullscreenerror on the relevant target. When the event occurs, the callback receives eventDetails, updates the status message and logs the event name and target in Developer Tools.
Try a blocked or unsupported fullscreen request and inspect the rejection.
Try it in your browser
Open the page, show the Console panel, and follow this test: Try a blocked or unsupported fullscreen request and inspect the rejection. Then change one thing in the example and observe whether the event still fires.
Common Beginner Mistakes
- Expecting
fullscreenerrorto fire without its required user action or browser condition. - Try a blocked or unsupported fullscreen request and inspect the rejection.
- Adding repeated event listeners without considering cleanup or unwanted side effects.
Key Takeaways
fullscreenerrorreports an attempt to enter fullscreen fails.addEventListener("fullscreenerror", ...)listens for the event on the appropriate target.- Try a blocked or unsupported fullscreen request and inspect the rejection.