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 toggle; not just to enter fullscreen mode, but to exit it if the user is in fullscreen. It goes into fullscreen mode perfectly, and executes the exit fullscreen code (as the alert()
box is shown which runs after the exit code but inside the exit code condition only) yet it does nothing.
I have read up on this here, and here which seems that I am doing everything correct, but something is missing. Could you please assist in figuring out how to make this procedural code work.
function fullscreen() {
var isInFullScreen = (document.fullscreenElement && document.fullscreenElement !== null) ||
(document.webkitFullscreenElement && document.webkitFullscreenElement !== null) ||
(document.mozFullScreenElement && document.mozFullScreenElement !== null) ||
(document.msFullscreenElement && document.msFullscreenElement !== null);
var docElm = document.documentElement;
if (!isInFullScreen) {
if (docElm.requestFullscreen) {
docElm.requestFullscreen();
} else if (docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen();
} else if (docElm.webkitRequestFullScreen) {
docElm.webkitRequestFullScreen();
} else if (docElm.msRequestFullscreen) {
docElm.msRequestFullscreen();
}
} else {
if (docElm.exitFullscreen) {
docElm.exitFullscreen();
} else if (docElm.webkitExitFullscreen) {
docElm.webkitExitFullscreen();
} else if (docElm.mozCancelFullScreen) {
docElm.mozCancelFullScreen();
} else if (docElm.msExitFullscreen) {
docElm.msExitFullscreen();
}
alert('exit fullscreen, doesnt work');
}
}
<a onclick="fullscreen()" href="javascript:void(0);">Toggle FullScreen</a>
I also tried adding parent.exitfs()
where the alert code is, according to this questions accepted answer and still has no impact