-1

I have a code like this:

var IFS =
    document.isFullScreen || 
    document.webkitIsFullScreen || 
    document.mozIsFullScreen || 
    document.msIsFullScreen;

The exact problem is that the || gates sorts false as undefined, since isFullScreen is a boolean.

How do I fix this?

2 Answers2

1

If every element is undefined, then IFS will be undefined. Since undefined is a falsy value, you can still make conditional statements as:

var IFS =
    document.isFullScreen || 
    document.webkitIsFullScreen || 
    document.mozIsFullScreen || 
    document.msIsFullScreen;

if(!IFS){
    console.log('not full screen');
}

If you still want to hold the false value in case every other variable is undefined, then you can go with something like:

var IFS =
    document.isFullScreen || 
    document.webkitIsFullScreen || 
    document.mozIsFullScreen || 
    document.msIsFullScreen || 
    false;
mati
  • 5,218
  • 3
  • 32
  • 49
-1

undefined is a falsey value, meaning the || operator will convert will consider it the same as false.

If you just want to find the first value which is not defined you could try something like this:

var IFS =
    (typeof document.isFullScreen != "undefined") ? document.isFullScreen :
    (typeof document.webkitIsFullScreen != "undefined") ? document.webkitIsFullScreen :
    (typeof document.mozIsFullScreen != "undefined") ? document.mozIsFullScreen :
    (typeof document.msIsFullScreen != "undefined") ? document.msIsFullScreen : 
    false;

Or as Niet suggests:

var IFS =
    ('isFullScreen' in document) ? document.isFullScreen :
    ('webkitIsFullScreen' in document) ? document.webkitIsFullScreen :
    ('mozIsFullScreen' in document) ? document.mozIsFullScreen :
    ('msIsFullScreen' in document) ? document.msIsFullScreen : 
    false;

Or you could use an array, like this:

var arr = ['isFullScreen', 'webkitIsFullScreen', 'mozIsFullScreen', 'msIsFullScreen'];
var IFS = false;
for (var i = 0; i < arr.length; i++) {
    if (arr[i] in document) {
        IFS = document[arr[i]];
        break;
    }
}

Or like this

var arr = ['isFullScreen', 'webkitIsFullScreen', 'mozIsFullScreen', 'msIsFullScreen'];
var key = arr.filter(function(e) { return e in document; })[0];
var IFS = !!document[key];
Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331