Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set window to fullscreen (REAL fullscreen; F11 functionality) by javascript

There are several questions about this, some say it's not possible, some say it IS possible in IE such as Internet Explorer full screen mode? and I'm wondering a universal solution and an answer for this.

I'm building a photo gallery webpage, and the gallery really makes out a difference when viewed fullscreen (as the title says, I am talking about true fullscreen, not with bars and window chrome etc), and I would like to place a button for fullscreen. (no, I won't be going forcefully FS without user's intention, I hate that kind of "functionality" too) It IS possible in Flash when initiated through a user-initiated action such as button click, and I was wondering if such a thing is available for Javascript too. Logically, it should have a mechanism similar to Flash/SL user initiated fullscreen mode. If there's no "universal" functionality that will work for all, I'm ok (better than nothing) for partial functionality (I mean supporting SOME of the browsers by that, NOT setting window width/height etc. don't come with answer telling to set window width/height, I know how to do it, I'm NOT looking for it) too.

like image 447
Can Poyrazoğlu Avatar asked Aug 24 '11 17:08

Can Poyrazoğlu


People also ask

How do I get full screen with F11?

On Windows 10 and Windows 11, the easiest method to make Microsoft Edge full-screen is by pressing the F11 key on your keyboard. Or, if you use a laptop or a device with the Fn key on its keyboard, use the Fn + F11 keys.

How do I get full screen without F11?

Hold down the Ctrl key (or the Command key on a Mac) and press the plus or minus keys on the keyboard to zoom in and out, respectively.

How do I make HTML fullscreen?

Switching to fullscreen mode is done by calling Element. requestFullscreen() on the <video> element. If fullscreen mode is already active ( fullscreenElement is not null ), we call exitFullscreen() on the document to shut off fullscreen mode.


1 Answers

This is now possible in the latest versions of Chrome, Firefox and IE(11).

Following the pointers by Zuul on this thread, I edited his code to include IE11 and the option to full screen any element of choice on your page.

JS:

function toggleFullScreen(elem) {     // ## The below if statement seems to work better ## if ((document.fullScreenElement && document.fullScreenElement !== null) || (document.msfullscreenElement && document.msfullscreenElement !== null) || (!document.mozFullScreen && !document.webkitIsFullScreen)) {     if ((document.fullScreenElement !== undefined && document.fullScreenElement === null) || (document.msFullscreenElement !== undefined && document.msFullscreenElement === null) || (document.mozFullScreen !== undefined && !document.mozFullScreen) || (document.webkitIsFullScreen !== undefined && !document.webkitIsFullScreen)) {         if (elem.requestFullScreen) {             elem.requestFullScreen();         } else if (elem.mozRequestFullScreen) {             elem.mozRequestFullScreen();         } else if (elem.webkitRequestFullScreen) {             elem.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);         } else if (elem.msRequestFullscreen) {             elem.msRequestFullscreen();         }     } else {         if (document.cancelFullScreen) {             document.cancelFullScreen();         } else if (document.mozCancelFullScreen) {             document.mozCancelFullScreen();         } else if (document.webkitCancelFullScreen) {             document.webkitCancelFullScreen();         } else if (document.msExitFullscreen) {             document.msExitFullscreen();         }     } } 

HTML:

<input type="button" value="click to toggle fullscreen" onclick="toggleFullScreen(document.body)"> 

Where "document.body" is any element you so wish.

Also note that trying to run these full screen commands from the console do not appear to work on Chrome or IE. I did have success with Firebug in Firefox though.

One other thing to note is that these "full screen" commands don't have a vertical scrollbar, you need to specify this within the CSS:

*:fullscreen *:-ms-fullscreen, *:-webkit-full-screen, *:-moz-full-screen {    overflow: auto !important; } 

The "!important" seems to be necessary for IE to render it

Here's an example of it working.

A quick note for anyone wanting to edit this and turn it into a code snippet, don't bother. The code doesn't work from within SO code snippets because it puts it within an iframe.

like image 165
Jamie Barker Avatar answered Sep 19 '22 04:09

Jamie Barker