Reputation: 331
if (!document.fullscreenElement
&& !document.mozFullScreenElement
&& !document.webkitFullscreenElement
&& !document.msFullscreenElement) {
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
} else if (document.documentElement.msRequestFullscreen) {
document.documentElement.msRequestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
}
else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
I have this code for toggling full screen. It works, but it has some little issues.
When I'm clicking to the full screen button, after refresh full screen mode exits, but if I press F11 and refresh, full screen won't exit.
If full screen mode is enabled, after pressing F11, clicking on full screen button, will not work
How Can I fixed this problems?
Upvotes: 1
Views: 1895
Reputation: 29645
I don't know if you'll be able to fix those issues. They happen because there are two different full screen modes:
While you can control the first one with JS, you cannot control the other. This makes sense from a usability/security point of view: you should be able to control the behavior within your page, but not outside of it or the user's preferences.
The issues that you are facing happen because:
fullscreen
flag is unset. When you go to full screen mode with the API, you are setting that flag, but when you refresh the page, the flag goes to its original value (unset) and the full screen is lost. The same way that any other JavaScript variable would be reset when reloading a page.fullscreen
flag is set. You can see the difference in this demo by Robert Nyman. He has added CSS so the page will go red when on :fullscreen
. Press F11 and notice how the background doesn't turn red, now click the "Fullscreen/Cancel fullscreen" buttons to see how the background color changes.Upvotes: 4