Skip to content

Instantly share code, notes, and snippets.

@vinnycrazzy
Forked from demonixis/toggleFullscreen.js
Created September 6, 2021 19:04
Show Gist options
  • Save vinnycrazzy/26c79237247c6b3a46574e0a75060dbc to your computer and use it in GitHub Desktop.
Save vinnycrazzy/26c79237247c6b3a46574e0a75060dbc to your computer and use it in GitHub Desktop.
A simple function to toggle fullscreen in JavaScript. It work well on Firefox and Webkit browsers.
/**
* Toggle fullscreen function who work with webkit and firefox.
* @function toggleFullscreen
* @param {Object} event
*/
function toggleFullscreen(event) {
var element = document.body;
if (event instanceof HTMLElement) {
element = event;
}
var isFullscreen = document.webkitIsFullScreen || document.mozFullScreen || false;
element.requestFullScreen = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || function () { return false; };
document.cancelFullScreen = document.cancelFullScreen || document.webkitCancelFullScreen || document.mozCancelFullScreen || function () { return false; };
isFullscreen ? document.cancelFullScreen() : element.requestFullScreen();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment