Browser F11 Fullscreen Does Not Register with -webkit-full-screen or Javascript API

I am using javascript(for example, requestFullscreen) and css(:-webkit-full-screen) API's to detect the browser's state in full screen or not. I don't have a issue with these API's, as they work successfully.

The issue I am having is that if the user hits F11, it does not register in the browser's environment and the javascript api and CSS api's for fullscreen detection do not detect fullscreen. Is there any way to work around this? I have some animations that depend on the size of the screen(in regards to fullscreen) and I have come to a dead end.

Answers


Worth a look

(function() {  

    var checktimer;

    function isFullScreen() {
        return (window.fullScreen) || (window.innerWidth == screen.width && window.innerHeight == screen.height);
    }

    $(window).on('resize', function() { 
        if(typeof checktimer!="undefined") { clearTimeout(checktimer); }
        checktimer = setTimeout(function() { alert(isFullScreen()); },2000); 
    });

})();

Notice that the call to the isFullScreen() function is timed, this is because some browsers will animate the fullscreen action , causing a resize event to fire until the animate stops.

Tried on Firefox and Chrome - ff uses the window.fullScreen here, I have not looked at the others, but easy enough to add those in the check of the return.


You can use event listener fullscreenchange :

var isFullscreen = false;
document.addEventListener('fullscreenchange', function () {
    isFullscreen = !!document.fullscreen;
    yourFunction();
}, false);

document.addEventListener('mozfullscreenchange', function () {
    isFullscreen = !!document.mozFullScreen;
    yourFunction();
}, false);

document.addEventListener('webkitfullscreenchange', function () {
    isFullscreen = !!document.webkitIsFullScreen;
    yourFunction();
}, false);

function yourFunction() {
    if(isFullscreen) {
        $('.your-element').addClass('fullscreen');
        // ...
    } else {
        $('.your-element').removeClass('fullscreen');
        // ...
    }
}

Documentation:


As @bigBen suggested in his comment above, $(window).on('resize', function() { }); does detect F11, for me too (Chrome Version 33.0.1750.154 m).

However, with the way I use it there are multiple calls because of the on('keydown'/'keyup'), so as long as you use off('keydown'/'keyup') right after, it'll run your functions once.* *Correction The multiple calls were because of window.onresize, not keydown.

Here's a console screenshot where I logged entering and exiting via F11 using 'resize' http://i.stack.imgur.com/4ojr1.png

Old code:

$(document).on('keydown', function(event) {
    $(document).off('keydown');
    $(window).on('resize', function() {
        if ($('body').hasClass('fullscreenOn')) {
            $('body').removeClass('fullscreenOn');
            // Do functions when exiting fullscreen
            $(document).on('keydown'); // Turn keydown back on after functions
            console.log("Exit F11");
        } else {
            $('body').addClass('fullscreenOn');
            // Do functions when entering fullscreen
            $(document).on('keydown'); // Turn keydown back on after functions
            console.log("Enter F11");
        }
    });
});

Jsfiddle (old): http://jsfiddle.net/4b8VL/ (Caveat - need to click inside result area before first F11 keypress)

Using 'resize' has the added benefit of also detecting when entering fullscreen via JS API. Do note that if you're using fullscreen via JS API as well, you should hide the button for going fullscreen (via the JS API) whenever fullscreen is activated. Thus the user can only use the exit method given by the browser - Esc when fullscreen entered via JS API, or F11 when fullscreen entered via F11.

* Update * The following code is cross-browser friendly. Instead of detecting window.onresize and using that as a basis for toggling fullscreen, it's better to just redirect the F11 key to use the fullscreen API.

// fullscreen API goes here - **MDN - Using fullscreen mode -** https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode

$(document).on('fullscreenchange', function(event) { // 'fullscreenchange' eventlistener from fullscreen API
    event.preventDefault();
    if ($('body').hasClass('fullscreen')) {
        $('body').removeClass('fullscreen');
        // Do functions when exiting fullscreen
        console.log("Exit Fullscreen");
    } else {
        $('body').addClass('fullscreen');
        // Do functions when entering fullscreen
        console.log("Enter Fullscreen");
    }
});

$(document).on('keydown', function(event) {
    if (event.which == 122) {
        event.preventDefault();
        toggleFullScreen(); // From fullscreen API
    }
});

Note: This doesn't appear to work in jsfiddle, due to the way jsfiddle is setup, I believe . However, it works in live sites; check out a side project of mine at http://aaronkhare.com/playground/circa (turn your volume down/off) for a live, working example of the new code.


Check out this HTML5 Enter Fullscreen shim:

A shim is like a mini hack to make the action cross browser compatible (really a wedge or doorjam), until the browsers figure it out

// detecting if browser supports fullscreen
return document.body.mozRequestFullScreen 
    || document.body.webkitRequestFullScreen 
    || document.body.requestFullScreen;

// requesting full screen on an elm
( elm.mozRequestFullScreen && elm.mozRequestFullScreen() ) 
    || ( elm.webkitRequestFullScreen && elm.webkitRequestFullScreen() ) 
    || ( elm.requestFullScreen && elm.requestFullScreen() );

    //binding to full screen event
    ( document.body.requestFullScreen && 
        window.addEventListener('fullscreenchange',fullScreenEvent) ) || ( document.body.webkitRequestFullScreen && 
        window.addEventListener('webkitfullscreenchange',fullScreenEvent ) ) || ( document.body.mozRequestFullScreen && 
        window.addEventListener('mozfullscreenchange',fullScreenEvent) );

Source: https://gist.github.com/samccone/1653975/raw/0c5597e6bc8e312d0674ff25a67d3271916a957f/gistfile1.js


Need Your Help

CakePHP Error: Plugin Migrations could not be found

windows console migration cakedc cakephp-2.5

I installed CakeDC Migration Plugin and trying the command cake Migrations.migration generate, but got the error Error: Plugin Migrations could not be found.

Multiple Models in a single View

c# asp.net asp.net-mvc model

I'm new to MVC C# and still learning the basics.