Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Fullscreen API in JavaScript

Toggle a panel in JavaScript with requestFullscreen and exitFullscreen. Handle failed requests and fullscreenchange, including when Escape exits the display.

BeginnerJavaScript Browser APIsExample 4 of 15

Save this as index.html and open it in a browser.

fullscreen-api.js
1<!doctype html>
2<html lang="en">
3<meta charset="utf-8">
4<title>JavaScript practice</title>
5<body>
6<section id="panel"><h1>Revision slides</h1><button id="full">Toggle fullscreen</button><p id="status"></p></section>
7<script>
8const panel = document.querySelector('#panel');
9const status = document.querySelector('#status');
10document.querySelector('#full').onclick = async () => {
11 try {
12 if (document.fullscreenElement) await document.exitFullscreen();
13 else if (panel.requestFullscreen) await panel.requestFullscreen();
14 else status.textContent = 'Fullscreen not supported.';
15 } catch { status.textContent = 'Fullscreen request refused.'; }
16};
17document.addEventListener('fullscreenchange', () => {
18 status.textContent = document.fullscreenElement ? 'Fullscreen on' : 'Fullscreen off';
19});
20</script>
21</body>
22</html>

Sample Output

Fullscreen on

A fullscreenchange event confirmed that an element is fullscreen.

Fullscreen off

Escape or the toggle ended fullscreen, triggering another state event.

In this one

  • Enter fullscreen from a button
  • Exit fullscreen safely
  • Listen for actual state changes
  • Respect browser restrictions

How it works

  1. 1

    Select the panel and message element.

  2. 2

    On a click, inspect document.fullscreenElement.

  3. 3

    Call exitFullscreen or requestFullscreen and handle a refusal.

  4. 4

    Listen for fullscreenchange, including Escape-driven changes.

  5. 5

    Compare the screen size with the Fullscreen on or off message.

Edge cases

No direct click

An automatic request can be refused.

Keep the request in the click handler. Browsers protect people from pages taking over the screen unexpectedly.

Embedded page

A containing frame may block fullscreen.

The outer page must allow fullscreen. The inner script cannot grant itself that permission.

What's going on

The Fullscreen API asks the browser to let one element fill the display. An element is a part of the page, such as this revision panel. This can make slides easier to read without the rest of the browser window taking space.

Save the complete example as an HTML file and use a supported browser. The button checks the current fullscreen element. It either asks the panel to enter fullscreen or asks the document to leave it. These requests can fail, so the code waits for them inside try/catch.

The fullscreenchange event is a notice that the state actually changed. Use that event to update the message because the person can also leave with Escape. Do not remove that way out or start fullscreen on page load. Embedded pages may need permission from the page containing them.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What reports the active fullscreen element?

Pick an answer to check it.

Question 2

Why catch the request?

Pick an answer to check it.

Questions people ask

Does this make every page element fullscreen?

No. requestFullscreen is called on panel, so that panel is the fullscreen element.

Why listen for fullscreenchange?

Escape can end fullscreen without clicking your button. The event keeps the status correct.

Can I force fullscreen forever?

No. The browser and the person control whether fullscreen is allowed and when it ends.