1

I'm having some problems trying to use short circuiting on a web page I am making.

I am trying to use

document.webkitExitFullscreen() || document.mozCancelFullScreen() || document.exitFullScreen();

But it seems to stop on the first try, despite that I would have though it would continue after the first argument comes up as undefined.

If I simply type in

document.mozCancelFullScreen()

then it works fine

http://i.imgur.com/rINs1kR.png

I was wondering if anyone could point me to what I'm doing wrong here The screenshot is taken in firefox btw. Thanks in advance

krillgar
  • 12,596
  • 6
  • 50
  • 86
  • If you call `document.webkitExitFullScreen()` in Firefox, what does it return? Just there inside the console, enter the code, and see what the result is. If it returns `true`, then that is why it short circuits your ors. – krillgar Dec 22 '16 at 14:59
  • What exactly do you expect. The code you provide is the fullsreen API for exiting fullscreen and your image shows a screen that has just entered fullscreen – zer00ne Dec 22 '16 at 15:00
  • using it without parenthesis returns undefined – user3232009 Dec 22 '16 at 15:02
  • @zer00ne I'm trying to get it to exit fullscreen, When I call the code above when clicking the yellow X, it returns document.webkitExitFullScreen() is not a function – user3232009 Dec 22 '16 at 15:04
  • @krillgar (here is simply calling the webkit version)[http://i.imgur.com/xgxeYFh.png] – user3232009 Dec 22 '16 at 15:09

2 Answers2

4

Your code is trying to call document.webkitExitFullscreen and if it returns a falsy value, call document.mozCancelFullScreen, etc.

But if document.webkitExitFullscreen itself is undefined you'll get an error trying to call it, and the code will stop running at that point.

Perhaps:

var exitFullScreen = document.webkitExitFullscreen || document.mozCancelFullScreen || document.exitFullScreen;
if (exitFullScreen) {
    exitFullScreen.call(document); // Just `exitFullScreen();` may work as well
}

Or alternatively:

["webkitExitFullscreen", "mozCancelFullScreen", "exitFullScreen"].some(function(name) {
    if (document[name]) {
        document[name]();
        return true;
    }
});

...which avoids the whole "do I need call or not?" issue.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @user3232009: You probably couldn't when you posted that comment, there's a 15-minute blockout. You can now, by clicking the checkmark next to the answer. Glad that helped! – T.J. Crowder Dec 22 '16 at 15:40
2

The problem is that you're already calling the function, so if it doesn't exist, you get an error. You coudl try something like:

(document.webkitExitFullscreen || document.mozCancelFullScreen || document.exitFullScreen)();
Shilly
  • 8,511
  • 1
  • 18
  • 24