Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active December 21, 2015 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leodutra/6343705 to your computer and use it in GitHub Desktop.
Save leodutra/6343705 to your computer and use it in GitHub Desktop.
crossBrowserFullScreen.js - Try fullscreen mode on most browsers. HTML5 alternatives only work when bound to an interaction event (click/ keyboard/ etc).
/*
Copyright 2013 Leonardo Dutra Constancio.
https://gist.github.com/LeoDutra/ - leodutra.br@gmail.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var crossBrowserFullScreen = (function ()
{
"use strict";
var crossBrowserFullScreen;
function log(str, ex)
{
if (crossBrowserFullScreen.debug)
{
str = 'CrossBrowserFullScreen :: ' + str + (ex ? '\n(' + ex.message + ').' : '');
try
{
alert(str);
}
catch (str)
{}
}
}
crossBrowserFullScreen = {
debug: false,
maximize: function ()
{
var win = top.window;
try
{
win.moveTo(0, 0);
if (document.all) win.resizeTo(screen.availWidth, screen.availHeight);
else if (document.layers)
{
win.outerHeight = screen.availHeight;
win.outerWidth = screen.availWidth;
}
}
catch (win)
{
log('Could not maximize window.', win);
}
},
requestFullscreen: function (el)
{
el = el || document.element;
if (el.requestFullscreen) el.requestFullscreen(); // HTMl5 spec
else if (el.webkitRequestFullScreen)
{
if (navigator.userAgent.match(/\bchrome\b/i)) el.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
else el.webkitRequestFullScreen(); // Safari
}
else if (el.msRequestFullScreen) el.msRequestFullScreen(); // ie
else if (el.mozRequestFullScreen) el.mozRequestFullScreen(); // mozilla
else // old ie
{
var ex;
try
{
new ActiveXObject("WScript.Shell").SendKeys("{F11}"); // security settings dependent
}
catch (ex)
{
log('Could not send "F11" key.', ex);
this.maximize();
}
}
},
exitFullscreen: function ()
{
if (document.exitFullscreen) document.exitFullscreen(); // HTMl5 spec
else if (document.cancelFullScreen) document.cancelFullScreen();
else if (document.webkitCancelFullScreen) document.webkitCancelFullScreen();
else if (document.msExitFullscreen) document.msExitFullscreen(); // ie
else if (document.mozCancelFullScreen) document.mozCancelFullScreen(); // mozilla
else // old ie
{
var ex;
try
{
new ActiveXObject("WScript.Shell").SendKeys("{ESCAPE}"); // security settings dependent
}
catch (ex)
{
log('Could not send "escape" key.', ex);
}
}
},
isFullscreen: function ()
{
return !!(document.fullscreen || document.webkitIsFullScreen || document.mozFullScreen || document.msFullScreen || document.webkitFullScreen ||
/*OLD FF*/
window.fullscreen ||
/*OLD IE*/
(window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth | 0) == screen.width &&
(window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight | 0) == screen.height);
},
getFullscreenElement: function ()
{
return document.fullScreenElement || document.mozFullScreenElement || document.msFullscreenElement || document.webkitFullScreenElement || null;
},
isFullscreenEnabled: function ()
{
return !!(document.fullScreenEnabled || document.webkitFullscreenEnabled || document.msFullscreenEnabled || document.mozFullScreenEnabled);
},
toggleFullscreen: function (el)
{
if (this.isFullscreen()) this.exitFullscreen(el);
else this.requestFullscreen(el);
}
};
return crossBrowserFullScreen;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment