48
votes

Possible Duplicate:
Detecting if a browser is in full screen mode

Is there a way to check if a browser is currently in fullscreen mode (after the user pressed f11)?

Something like:

if (window.fullscreen) {
  // it's fullscreen!
}
else {
  // not fs!
}

Thanks.

Steerpike's answer is pretty good, but my comment:

Thanks a lot, but this answer is not sufficient for FF. In Chrome I can set a small tolerance, but in FF the urlbar and tabs takes a while to disappear, which means after pressing f11, the detected window.innerWidth is still too small.

6
You could also use document.fullscreenElement to get element if it exists else it will be null. Inner- and screen width are not reliable, for example, if you open dev tools the inner sizes will not be the same as screen sizes. You can assign document.onfullscreenchange to this method to get an instant response when changing between fullscreen and off.tscpp

6 Answers

33
votes

In Firefox 3, window.fullScreen works (https://developer.mozilla.org/en/DOM/window.fullScreen).

So, you could potentially do something like this:

if((window.fullScreen) ||
   (window.innerWidth == screen.width && window.innerHeight == screen.height)) {

} else {

}
34
votes

This works for all new browsers :

if (!window.screenTop && !window.screenY) {
    alert('Browser is in fullscreen');
}
8
votes
if(window.innerWidth == screen.width && window.innerHeight == screen.height) {

} else {

}

(Warning: The browser chrome may muck with the height comparisons but the width checks should be pretty spot on)

2
votes

I've ended up with following solution:

function _fullscreenEnabled() {
    // FF provides nice flag, maybe others will add support for this later on?
    if(window['fullScreen'] !== undefined) {
      return window.fullScreen;
    }
    // 5px height margin, just in case (needed by e.g. IE)
    var heightMargin = 5;
    if($.browser.webkit && /Apple Computer/.test(navigator.vendor)) {
      // Safari in full screen mode shows the navigation bar, 
      // which is 40px  
      heightMargin = 42;
    }
    return screen.width == window.innerWidth &&
        Math.abs(screen.height - window.innerHeight) < heightMargin;
  }

Which works in every browser I care about (Chrome, FF, IE, Safari/Mac, Opera).

Update: It doesn't work on Opera/Mac, Opera on Mac while in full screen mode hides only the 'common' OSX menu, thus height differs only by few pixels which is too dangerous for me.

0
votes

this works on major browsers (ie,ff,opera,chrome)

function isFullscreen(){

  if($.browser.opera){

    var fs=$('<div class="fullscreen"></div>');
    $('body').append(fs);

    var check=fs.css('display')=='block';
    fs.remove();

    return check;
  }

  var st=screen.top || screen.availTop || window.screenTop;

  if(st!=window.screenY){

    return false;
  }

  return window.fullScreen==true || screen.height-document.documentElement.clientHeight<=30;
}

css for opera:

.fullscreen { display: none; }

@media all and (view-mode: fullscreen){

  .fullscreen { display: block; }
}
-1
votes

Simple enough: Find the browser viewport using $(window).width() and $(window).height(), and if they conform to a set of defined viewport sizes (600 x 480, 1280 x 800, etc.), then you can know that it is most likely fullscreen. Also you can set event handlers for like the fll key and other possible shortcuts to define browser fullscreen.