Is there a better way for me to write the toggleFullscreen(). I’m repeating the style rules on every browser which seems very unnecessary.
function toggleFullScreen() {
var elem = document.getElementById("video_container");
var db = document.getElementById("defaultBar");
var ctrl = document.getElementById("controls");
if (!document.fullscreenElement && // alternative standard method
!document.mozFullScreenElement && !document.webkitFullscreenElement) { // current working methods
if (document.documentElement.requestFullscreen) {
db.style.background ='red';
ctrl.style.width = '50%';
ctrl.style.left = '25%';
elem.requestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
db.style.background ='red';
ctrl.style.width = '50%';
ctrl.style.left = '25%';
elem.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
db.style.background ='red';
ctrl.style.width = '50%';
ctrl.style.left = '25%';
elem.webkitRequestFullscreen();
}
} else if (document.exitFullscreen) {
db.style.background ='yellow';
ctrl.style.width = '100%';
ctrl.style.left = '0';
document.exitFullscreen();
}
else if (document.mozCancelFullScreen) {
db.style.background ='yellow';
ctrl.style.width = '100%';
ctrl.style.left = '0';
document.mozCancelFullScreen();
}
else if (document.webkitCancelFullScreen) {
db.style.background ='yellow';
ctrl.style.width = '100%';
ctrl.style.left = '0';
document.webkitCancelFullScreen();
}
}
That’s because of this code:
If immediately executes
toggleFullScreen()and passes the return value toaddEventListenerinstead. The code should probably read:This code passes the reference to the function instead of its return value.
Refactoring
By using the
||operator you can greatly simplify the existing conditions.