How to detect when a page exits fullscreen?
Here's how I did it:
if (document.addEventListener)
{
document.addEventListener('fullscreenchange', exitHandler, false);
document.addEventListener('mozfullscreenchange', exitHandler, false);
document.addEventListener('MSFullscreenChange', exitHandler, false);
document.addEventListener('webkitfullscreenchange', exitHandler, false);
}
function exitHandler()
{
if (!document.webkitIsFullScreen && !document.mozFullScreen && !document.msFullscreenElement)
{
// Run code on exit
}
}
Supports Opera, Safari, Chrome with webkit
, Firefox/Gecko with moz
, IE 11 with MSFullScreenChange
, and will support the actual spec with fullscreenchange
once it's been successfully implemented in all of the browsers. Obviously, Fullscreen API is only supported in the modern browsers, so I did not provide attachEvent
fallbacks for older versions of IE.
API for browsers changed.
For example: there is no document.webkitIsFullScreen
in Chrome. This is what worked for me:
document.addEventListener('fullscreenchange', onFullScreenChange, false);
document.addEventListener('webkitfullscreenchange', onFullScreenChange, false);
document.addEventListener('mozfullscreenchange', onFullScreenChange, false);
function onFullScreenChange() {
var fullscreenElement =
document.fullscreenElement ||
document.mozFullScreenElement ||
document.webkitFullscreenElement;
// if in fullscreen mode fullscreenElement won't be null
}
The Fullscreen spec specifies that the "fullscreenchange"
(with the appropriate prefix) event is fired on the document any time the fullscreen state of the page changes, that includes going into and out of full screen. Inside that event you can check document.fullScreenElement
to see if the page is fullscreen or not. If it's fullscreen the property will be non-null.
Check out MDN for more details.
As for your other questions: No, there is no way to prevent Esc
from exiting fullscreen. That's the compromise that was made to ensure that the user always has control over what their browser is doing. The browser will never give you a way to prevent users from exiting fullscreen. Period.
As for Firefox being slower at rendering than Chrome, I can assure you that for most practical purposes it's not. If you're seeing a large difference in performance between the two browsers that probably means your javascript code is the bottleneck, not the GPU.