Not sure if the following code snip will work embedded on SO, as it didn\'t work when pasting it, however it does work stand-alone.
The problem, is I want this to be a t
Krang's answer was great, Carl's idea to modularize was too. But I couldn't easily understand his code. So here's my version in TypeScript:
function isInFullScreen() {
const document: any = window.document;
return (document.fullscreenElement && document.fullscreenElement !== null) ||
(document.webkitFullscreenElement && document.webkitFullscreenElement !== null) ||
(document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
(document.msFullscreenElement && document.msFullscreenElement !== null);
}
function requestFullScreen(elem: any) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullScreen) {
elem.webkitRequestFullScreen();
} else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen();
} else {
console.warn("Did not find a requestFullScreen method on this element", elem);
}
}
function exitFullScreen() {
const document: any = window.document;
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
function toggleFullScreen(elem: any) {
// based on https://stackoverflow.com/questions/36672561/how-to-exit-fullscreen-onclick-using-javascript
if (isInFullScreen()) {
exitFullScreen();
} else {
requestFullScreen(elem || document.body);
}
}