Detect fullscreen mode
Modern desktop version of IE 10 is always fullscreen.
There is a living specification for :fullscreen
pseudo-class on W3
But when I tried to detect fullscreen with jQuery version 1.9.x and 2.x:
$(document).is(":fullscreen")
it threw this error:
Syntax error, unrecognized expression: fullscreen
Questions:
Is it because jQuery doesn't recognize this standard yet or IE10?
-
What is the legacy way of checking fullscreen mode? I am expecting following results:
function IsFullScreen() { /* Retrun TRUE */ /* If UA exists in classic desktop and not in full screen */ /* Else return FALSE */ }
Can we do it without browser sniffing?
Solution 1:
As you have discovered, browser compatibility is a big drawback. After all, this is something very new.
However, since you're working in JavaScript, you have far more options available to you than if you were just using CSS.
For example:
if( window.innerHeight == screen.height) {
// browser is fullscreen
}
You can also check for some slightly more loose comparisons:
if( (screen.availHeight || screen.height-30) <= window.innerHeight) {
// browser is almost certainly fullscreen
}
Solution 2:
an event is fired when the browser changes full screen mode. You can use that to set a variable value, which you can check to determine if the browser is full screen or not.
this.fullScreenMode = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen; // This will return true or false depending on if it's full screen or not.
$(document).on ('mozfullscreenchange webkitfullscreenchange fullscreenchange',function(){
this.fullScreenMode = !this.fullScreenMode;
//-Check for full screen mode and do something..
simulateFullScreen();
});
var simulateFullScreen = function() {
if(this.fullScreenMode) {
docElm = document.documentElement
if (docElm.requestFullscreen)
docElm.requestFullscreen()
else{
if (docElm.mozRequestFullScreen)
docElm.mozRequestFullScreen()
else{
if (docElm.webkitRequestFullScreen)
docElm.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT)
}
}
}else{
if (document.exitFullscreen)
document.exitFullscreen()
else{
if (document.mozCancelFullScreen)
document.mozCancelFullScreen()
else{
if (document.webkitCancelFullScreen)
document.webkitCancelFullScreen();
}
}
}
this.fullScreenMode= !this.fullScreenMode
}