16

TLDR;

I can detect if the browser has gone to full screen through the full screen API but I cannot detect that the browser has gone to full screen through f11 or the browser menu (specifically chrome).

Original:

Currently I am using screenfull to go to full screen and detect that the browser is in full screen. The problem is that I do not want to display my full screen toggle button when the browser has gone to full screen through a browser function (i.e. f11 or full screen through the browser menus). This is because the javascript full screen API does not seem to be able to detect that you are in full screen or get you out of full screen when you have gone there through a browser function. I could just detect if f11 was hit, but this doesn't work on mac or when full screen has been initiated through the browser menus.

Any ideas on how to detect if full screen was initiated through a browser function? I'm only targeting webgl compatible browsers so that cuts down on a lot of gotchas.

Braden
  • 617
  • 1
  • 8
  • 23
  • 4
    Possible duplicate of [Detecting if a browser is in full screen mode](http://stackoverflow.com/questions/1047319/detecting-if-a-browser-is-in-full-screen-mode) – thanksd Dec 22 '15 at 18:17
  • http://stackoverflow.com/questions/5617963/how-to-detect-if-user-has-enabled-full-screen-in-browser – mike.zimin Dec 22 '15 at 18:18
  • I can detect if the browser has gone to full screen from the full screen API, but I cannot detect if the browser has gone to full screen from f11 or the toolbar menus – Braden Dec 22 '15 at 18:26
  • 1
    I don't know how reliable it would be, but could checking if screen.width and window.innerWidth are the same value help? For example, checking the values after a resize event. – Antonio Manente Dec 22 '15 at 18:36
  • 1
    I thought of the same thing as @AntonioManente, compare screen size and the browser window's size and you should know when it's fullscreen. You can try here: http://whatsmy.browsersize.com/ – Shanoor Dec 22 '15 at 18:49
  • Possible duplicate of [How to detect if user has enabled full screen in browser](http://stackoverflow.com/questions/5617963/how-to-detect-if-user-has-enabled-full-screen-in-browser) – Suma Feb 02 '17 at 19:38

3 Answers3

9

I haven't tested this for reliability but this is my take.

  //without jQuery
window.addEventListener('resize', function(){
  if(screen.width === window.innerWidth){
   // this is full screen
  }
});


  //with jQuery
$(document).ready(function() {
  $(window).on('resize', function(){
    if(screen.width === window.innerWidth){
      // this is full screen
    }
  });
});

This seems to work when pressing the F11 button and other methods, so it should catch the edge cases that the full screen api does not. Although I'm not sure the comparison of screen.width vs. window.innerWidth is a reliable way to check for full screen. Maybe someone else can add to/critique this.

Antonio Manente
  • 359
  • 2
  • 12
  • 5
    It's not enough to compare the width, it's the same when the window is maximized. You have to compare heights too but the fun begins here. In fullscreen Chromium, the screen height and window height are the same, in IE, it's not because of the status bar. I didn't try in firefox but status bar can be added with an extension. In fullscreen, the window height should be less than x% smaller than the screen size. – Shanoor Dec 22 '15 at 18:55
  • @ShanShan I see your point, although I have a question. OP was looking for edge cases where full screen was achieved through pressing 'F11' or a browser function. If I'm not mistaken, isn't the status bar compressed when getting to full screen this way? – Antonio Manente Dec 22 '15 at 19:03
  • IE always show the status bar even in full screen (I tried few minutes ago) unless you totally disable it. And I tried in firefox too, there is one pixel difference. This solution is a good start anyway, it only requires some tweaking and testing. – Shanoor Dec 22 '15 at 19:10
  • @ShanShan Yes, I questioned the reliability of this method when I though of it. Not a complete answer, but as you said... A good place to start – Antonio Manente Dec 22 '15 at 19:19
  • 1
    This seemed to work. Strangely though you cant have your chrome dev tools docked because then it substracts the size of that window. – Braden Dec 22 '15 at 20:34
  • @Tbonebrad I noticed that as well, as discussed in the previous comments, it isn't the most reliable but it may serve to catch the cases where the full screen api fails – Antonio Manente Dec 22 '15 at 21:01
  • 1
    This solution fails when the console is open –  Dec 29 '21 at 08:03
7

Use fullscreenchange event to detect a fullscreen change event, or if you don't want to handle vendor prefixes than you can also listen to the resize event (the window resize event that also triggers when fullscreen is entered or exited) and then check if document.fullscreenElement is not null to determine if fullscreen mode is on. You'll need to vendor prefix fullscreenElement accordingly. I would use something like this:

var fullscreenElement = document.fullscreenElement || document.mozFullScreenElement ||
document.webkitFullscreenElement || document.msFullscreenElement;

https://msdn.microsoft.com/en-us/library/dn312066(v=vs.85).aspx has a good example for this which I quote below. They have used the fullscreenChange event, but you could listen for the "resize" event

document.addEventListener("fullscreenChange", function () {
          if (fullscreenElement != null) {
              console.info("Went full screen");
          } else {
              console.info("Exited full screen");              
          }
      });
Parth
  • 1,114
  • 3
  • 16
  • 25
  • 7
    This doesn't actually work, as the `fullscreenChange` event only fires if you call the HTML5 fullscreen JavaScript API. If a user hits F11, that event doesn't fire and `document.fullscreenElement` (and the vendor prefix versions) remains `null`. – Kevin Ji Jun 06 '16 at 22:32
  • I'm getting document.fullscreenElement === undefined – Akin Hwan May 22 '18 at 15:12
7

After trying a lot of approaches across different browsers and devices, the following solution worked for me reliably.

 window.addEventListener("resize", () => {
        setTimeout(() => {
            const windowWidth = window.innerWidth * window.devicePixelRatio;
            const windowHeight = window.innerHeight * window.devicePixelRatio;
            const screenWidth = window.screen.width;
            const screenHeight = window.screen.height;
            console.log(windowWidth/screenWidth);
            console.log(windowHeight/screenHeight);
            if (((windowWidth/screenWidth)>=0.95) && ((windowHeight/screenHeight)>=0.95)) {
                console.log("Fullscreen");
            }
            else {
                console.log("Not Fullscreen");
            }
        }, 100);
    })
Aruldd
  • 489
  • 6
  • 6
  • 2
    To ensure you catch users who have zoomed in/out, be sure to include `window.devicePixelRatio` in the mix. The following variables should be changed: `const windowWidth = window.innerWidth * window.devicePixelRatio;` `const windowHeight = window.innerHeight * window.devicePixelRatio;` – duckboy81 Nov 28 '19 at 01:42