JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
213: The Fullscreen API
Imagine you're in a classroom with a laptop and a massive projector. Most of the time, you're just typing away on your small screen. But when it's time for the actual presentation, you hit a switch and suddenly, one specific slide fills the entire wall. You aren't moving the laptop; you're just telling the system, "Take this one piece of content and make it the only thing anyone sees."
That's exactly what the Fullscreen API does for your web app. The "laptop" is your browser window, the "slide" is a DOM element (like a <video> or a <div>), and the "switch" is a JavaScript method that tells the browser to ignore everything else and expand that element to the edges of the physical monitor.
Grabbing the whole screen
To make an element go fullscreen, you call requestFullscreen() on that specific element. It's an asynchronous method that returns a Promise, which is important if you're handling errors (like if the browser blocks the request).
const galleryImage = document.querySelector('.hero-image');
async function enterCinemaMode() {
try {
await galleryImage.requestFullscreen();
console.log("We're in the big screen now!");
} catch (err) {
console.error(`Error attempting to enable full-screen mode: ${err.message}`);
}
}
One thing to notice here: I called this on galleryImage. If you call it on a <div> that contains a video and some captions, the entire container goes fullscreen. If you call it only on the video, the captions will disappear from view. Choose your target wisely.
The "User Gesture" hurdle
Now, here is where most developers hit a wall. You cannot just trigger fullscreen on page load. If websites could do that, every annoying ad would suddenly take over your entire monitor the second you landed on a page. Absolute chaos.
The browser requires a "user gesture"—meaning a click, a keypress, or a touch. If you try to call requestFullscreen() inside a setTimeout or an API fetch response without a direct user trigger, the browser will throw a security error and refuse to comply. Always tie your fullscreen logic to a button or a specific interaction.
Getting back to reality
Exiting fullscreen is slightly different. While you requested fullscreen on a specific element, you exit fullscreen on the document object itself. You don't tell the image to shrink; you tell the browser to stop the fullscreen mode entirely.
function exitCinemaMode() {
if (document.fullscreenElement) {
document.exitFullscreen();
}
}
I usually check for document.fullscreenElement first. This property is your best friend—it returns the element that is currently in fullscreen mode, or null if the browser is in normal windowed mode. It's the easiest way to toggle your UI buttons between "Enter Fullscreen" and "Exit Fullscreen."
Listening for the switch
Since users can exit fullscreen by hitting the Esc key, your JavaScript needs to know when that happens so you can update your UI. You don't want a button that says "Enter Fullscreen" when the user is already staring at a full-screen image.
We use the fullscreenchange event for this. It's fired on the document whenever the state changes, regardless of whether it was triggered by your code or the Esc key.
document.addEventListener('fullscreenchange', () => {
if (document.fullscreenElement) {
console.log("The user is now in fullscreen mode.");
// Update your button text to "Exit"
} else {
console.log("The user returned to the window.");
// Update your button text to "Enter"
}
});📋 Practical Task
Build a Cinematic Image Toggle
Create a simple page with an image and a button. Your task is to implement a "Cinema Mode" toggle. The logic should be as follows:
- When the button is clicked, if the image is not in fullscreen, make it go fullscreen.
- If the image is already in fullscreen, exit fullscreen mode.
- The button text must dynamically change between "Go Cinematic" and "Back to Page" based on the current state.
- Crucial: The button text must update correctly even if the user presses the
Esckey to exit fullscreen, not just when they click the button.
There are no comments for now.