Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Fullscreen Event Listener

I'm trying to detect if the current document is fullscreen or not using:

document.addEventListener('webkitfullscreenchange mozfullscreenchange fullscreenchange', function(e) {
    if ((document.fullScreenElement && document.fullScreenElement !== null) || 
    (!document.mozFullScreenElement && !document.webkitFullScreenElement)) {
        console.log('fullscreen');
    } else {
        console.log('not fullscreen');
    }
}, false);

But the event NEVER gets fired no matter I do to enter or exit fullscreen.

Any ideas?

like image 960
Cameron Avatar asked Feb 13 '14 21:02

Cameron


People also ask

How do I make HTML fullscreen?

Switching to fullscreen mode is done by calling Element. requestFullscreen() on the <video> element. If fullscreen mode is already active ( fullscreenElement is not null ), we call exitFullscreen() on the document to shut off fullscreen mode.

Does fullscreen API require user permission?

Basics of the Fullscreen API The word “request” in requestFullscreen is due to the fact that browsers don't have permissions (by default) to activate fullscreen mode. Using the above function, to activate fullscreen, simply pass the document HTMLElement!

How do I change my full screen?

Use a keyboard shortcut to switch between full screen and normal display modes. When screen space is at a premium and you only need SecureCRT on your screen, press ALT+ENTER (Windows) or COMMAND+ENTER (Mac). The application will expand to full screen, hiding the menu bar, tool bar, and title bar.

How do I automatically open a web page in full screen mode in HTML?

Full-screen can be activated for the whole browser window by pressing the F11 key. It can be exited by pressing the Esc button. It is also possible to make a specific element in the page to enter and exit full-screen mode programmatically using Javascript Fullscreen API.


2 Answers

A convenient library-free solution:

["fullscreenchange", "webkitfullscreenchange", "mozfullscreenchange", "msfullscreenchange"].forEach(
    eventType => document.addEventListener(eventType, yourCheckFunction, false)
);

or alternatively:

["", "webkit", "moz", "ms"].forEach(
    prefix => document.addEventListener(prefix+"fullscreenchange", yourCheckFunction, false)
);
like image 160
Christoph142 Avatar answered Oct 12 '22 12:10

Christoph142


Are you sure you didn't mean to use the jQuery bind?

The document.addEventListener does not support multiple events as far as I know.

Use this example (based on this question on SO) if you like to use jQuery:

$(document).on('webkitfullscreenchange mozfullscreenchange fullscreenchange', function(e)
{
    //do something;
});
like image 26
Patrick Hofman Avatar answered Oct 12 '22 13:10

Patrick Hofman