Last updated: Mar 7, 2024
Reading time·4 min
To detect fullscreen mode in JavaScript:
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box">bobbyhadz.com</div> <script type="module" src="index.js"></script> </body> </html>
And here is the related JavaScript code.
if (window.innerHeight === screen.height) { console.log('The browser is in fullscreen mode'); } else { console.log('The browser is NOT in fullscreen mode'); }
Here is a short clip that demonstrates how this works.
You can press F11
to toggle fullscreen mode on most browsers.
The window.innerHeight property returns the interior height of the window in pixels, including the height of the horizontal scroll bar (if present).
The screen.height property returns the height of the screen in pixels.
If the interior height of the window is equal to the height of the screen, then the browser is in fullscreen mode.
resize
eventIf you want to detect whether the browser is in fullscreen mode dynamically, use
the resize
event.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box">bobbyhadz.com</div> <script type="module" src="index.js"></script> </body> </html>
Here is the related JavaScript code.
window.addEventListener('resize', function handleResize(event) { if (window.innerHeight === screen.height) { console.log('The browser is in fullscreen mode'); } else { console.log('The browser is NOT in fullscreen mode'); } });
The code sample is very similar to the one in the previous subheading.
We check if the max height of the screen is equal to the current height.
If the condition is met, the browser is in fullscreen mode.
We used the document.addEventListener method to track the resize event.
The resize
event is triggered when the document view has been resized.
Here is a short clip that demonstrates how this works.
Pressing F11
on my keyboard toggles the fullscreen mode.
If the browser is in fullscreen mode, the if
block runs, otherwise, the else
block runs.
If you need to toggle fullscreen mode when a user presses a certain key, e.g.
Enter
, set up a
keydown event listener.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box">bobbyhadz.com</div> <script type="module" src="index.js"></script> </body> </html>
And here is the related JavaScript code.
document.addEventListener( 'keydown', event => { if (event.key === 'Enter') { toggleFullScreenMode(); } }, false, ); function toggleFullScreenMode() { if (document.fullscreenElement == null) { console.log('Switching to fullscreen mode'); document.documentElement.requestFullscreen(); } else if (document.exitFullscreen) { console.log('Exiting fullscreen mode'); document.exitFullscreen(); } }
The keydown
event is triggered when a key is pressed.
I've written a detailed article on how to detect when the Enter key is pressed in React.
When the user presses Enter
, we toggle fullscreen mode in the browser.
The requestFullscreen() method issues an async request to make the element on which it was called fullscreen mode.
We called the method on the
documentElement
property of the document
object.
The property returns the root element of the document (the html
element).
function toggleFullScreenMode() { if (document.fullscreenElement == null) { console.log('Switching to fullscreen mode'); document.documentElement.requestFullscreen(); } else if (document.exitFullscreen) { console.log('Exiting fullscreen mode'); document.exitFullscreen(); } }
The
document.fullscreenElement
property returns the element that is currently rendered in fullscreen mode in
the document or null
if the document is not in fullscreen mode.
If the document is not in fullscreen mode, we use the requestFullscreen
method
to transition the browser to fullscreen mode.
Otherwise, we use the exitFullscreen() method to exit fullscreen mode.
fullscreenchange
eventYou can also use the fullscreenchange event to detect fullscreen mode.
The event is triggered after the browser switches into or out of fullscreen mode.
However, note that the event isn't triggered when the user presses F11
.
Rather, it is triggered when you use the HTML5 fullscreen JavaScript API.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box">bobbyhadz.com</div> <button id="toggle-fullscreen-btn"> Toggle fullscreen mode </button> <script type="module" src="index.js"></script> </body> </html>
And here is the related JavaScript code.
document.addEventListener('fullscreenchange', function () { if (document.fullscreenElement != null) { console.log('You are in fullscreen mode'); } else { console.log('Exited fullscreen mode'); } }); function toggleFullScreenMode(event) { if (document.fullscreenElement == null) { document.documentElement.requestFullscreen(); } else if (document.exitFullscreen) { document.exitFullscreen(); } } const button = document.getElementById('toggle-fullscreen-btn'); button.addEventListener('click', toggleFullScreenMode);
Here is a short clip that demonstrates how this works.
When the user clicks on the button, the toggleFullScreenMode
function is
invoked and it toggles fullscreen mode.
This triggers the fullscreenchange
event and prints a message to the console.
However, note that the fullscreenchange
event wouldn't trigger if you press
F11
.
The event is only triggered when you use the HTML5 fullscreen JavaScript API.
If you want to also track F11
key presses, use the resize
event from the
previous subheading.
I've also written an article on how to open a link in a new tab on button click.
You can learn more about the related topics by checking out the following tutorials: