Last active
August 29, 2015 14:21
-
-
Save ludo6577/53691fd3e71204f2ff36 to your computer and use it in GitHub Desktop.
FullScreen in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
/* | |
* Make the body go full screen. | |
* see: http://stackoverflow.com/questions/1125084/how-to-make-in-javascript-full-screen-windows-stretching-all-over-the-screen | |
*/ | |
function requestFullScreen() { | |
var element; | |
var requestMethod; | |
var isInFullScreen = document.fullScreenElement || document.mozFullScreen || document.webkitIsFullScreen || document.msIsFullScreen; | |
if (!isInFullScreen) { | |
element = document.body; | |
requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullscreen || element.msRequestFullScreen; | |
} else { | |
element = document; | |
requestMethod = element.cancelFullScreen || element.webkitCancelFullScreen || element.mozCancelFullScreen || element.msCancelFullscreen || element.msCancelFullScreen; | |
} | |
if (requestMethod) { // Native full screen. | |
requestMethod.call(element); | |
} else if (typeof window.ActiveXObject !== "undefined") { // Older IE. | |
var wscript = new ActiveXObject("WScript.Shell"); | |
if (wscript !== null) { | |
wscript.SendKeys("{F11}"); | |
} | |
} | |
} | |
</script> | |
<button id="fullscreen-button" onclick="requestFullScreen();"></button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requestFullScreen: function() { | |
/* | |
* Exit the fullscreen mode (reset divs styles) | |
*/ | |
function exitHandler() { | |
if (AMZ.Utils.Resizing) | |
return; | |
//var isInFullScreen = document.fullScreenElement || document.mozFullScreen || document.webkitIsFullScreen || document.msIsFullScreen; | |
//if (!isInFullScreen) | |
// return; | |
//The content div | |
var element = document.getElementById(AMZ.Statics.divIdToFullScreen); | |
if (element && AMZ.Utils.oldDivStyle) | |
element.setAttribute("style", AMZ.Utils.oldDivStyle); | |
//ExtJs component | |
var extComponent; | |
var renderElement = document.getElementById(AMZ.Statics.renderToComponentName); | |
if (renderElement && AMZ.Utils.oldComponentDivStyle) { | |
renderElement.setAttribute("style", AMZ.Utils.oldComponentDivStyle); | |
extComponent = Ext.getCmp(renderElement.children[0].id); | |
} | |
element = document; | |
var requestMethod = element.cancelFullScreen || element.webkitCancelFullScreen || element.mozCancelFullScreen || element.msCancelFullscreen || element.msCancelFullScreen; | |
_requestFullScreen(requestMethod, element, extComponent); | |
} | |
/* | |
* Enter the fullscreen mode (set the div style to 100%) | |
*/ | |
function enterHandler() { | |
//The content div | |
var element = document.getElementById(AMZ.Statics.divIdToFullScreen); | |
if (element) { | |
AMZ.Utils.oldDivStyle = element.getAttribute("style"); | |
element.setAttribute("style", "width:100%; height:100%"); | |
} else { | |
element = document.body; | |
} | |
//ExtJS Component | |
var extComponent; | |
var renderElement = document.getElementById(AMZ.Statics.renderToComponentName); | |
if (renderElement && renderElement.children[0]) { | |
AMZ.Utils.oldComponentDivStyle = renderElement.getAttribute("style"); | |
setTimeout(function () { renderElement.setAttribute("style", "width:100%; height:" + (element.offsetHeight - 50) + "px;"); }, 200); //element height is not updated directly | |
extComponent = Ext.getCmp(renderElement.children[0].id); | |
} | |
//element = document.body; | |
var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullscreen || element.msRequestFullScreen; | |
_requestFullScreen(requestMethod, element, extComponent); | |
} | |
/* | |
* Request the fullscreen to the browser | |
*/ | |
function _requestFullScreen(requestMethod, element, extComponent) { | |
if (requestMethod) { // Native full screen. | |
requestMethod.call(element); | |
} else if (typeof window.ActiveXObject !== "undefined") { // Older IE. | |
var wscript = new ActiveXObject("WScript.Shell"); | |
if (wscript !== null) { | |
wscript.SendKeys("{F11}"); | |
} | |
} | |
if (extComponent) { | |
setTimeout(function() { | |
extComponent.updateLayout(); | |
AMZ.Utils.Resizing = false; | |
}, 200); // i know... (we need to wait the div was resized) | |
} | |
} | |
/* | |
* Send the fullscreen request | |
*/ | |
var isInFullScreen = document.fullScreenElement || document.mozFullScreen || document.webkitIsFullScreen || document.msIsFullScreen; | |
if (!isInFullScreen) { | |
AMZ.Utils.Resizing = true; | |
enterHandler(); | |
} else { | |
exitHandler(); | |
} | |
/* | |
* Add listeners to the fullscreen change event (click on ESC) | |
*/ | |
if (document.addEventListener && !AMZ.Utils.fullScreenChangeEventIsRegistered) { | |
AMZ.Utils.fullScreenChangeEventIsRegistered = true; | |
document.addEventListener('webkitfullscreenchange', exitHandler, false); | |
document.addEventListener('mozfullscreenchange', exitHandler, false); | |
document.addEventListener('fullscreenchange', exitHandler, false); | |
document.addEventListener('MSFullscreenChange', exitHandler, false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment