22

I am looking for a trick to put my website in fullscreen mode without human interaction.

I've found some examples using HTML5's techniques, but all of then needs to be triggered by a human interaction.

This website will be displayed in a TV ...

I already think in load the website using a SWF file in fullscreen mode, but instead of going to this direction, I would like to stress all possibilities using just the default pattern (html, css and javascript)

Marcio Barroso
  • 783
  • 1
  • 7
  • 21

5 Answers5

28

You can't force a website to display in fullscreen mode.

Imagine the security concerns if that were possible.
Malicious websites could "Hijack" a less computer literate person's desktop for all kinds of dubious business.

All of JS fullscreen api's will throw a error like this:
"Failed to execute 'requestFullScreen' on 'Element': API can only be initiated by a user gesture." If you try to simply call it from your code.

I'm pretty darn sure Flash is similar, in that it requires user interaction to go fullscreen. Otherwise, we'd have seen our fair share of fullscreen popups that are nearly impossible to close.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 2
    Think about someone throwing together a web application that looks a lot like Windows (which is [entirely possible](http://dev.sencha.com/deploy/ext-4.0.1/examples/desktop/desktop.html)) and then opening up a "web browser" and asking for a Social Security number. I know that my mom wouldn't be able to tell the difference (she wouldn't enter her Social though, she knows better than that). – Sumner Evans Mar 26 '15 at 15:15
  • 3
    @jsve: That's _exactly_ what I mean with malicious intent. And that's _exactly_ why fullscreen isn't accessible without user interaction. – Cerbrus Mar 26 '15 at 15:16
  • 3
    Just a little question: what do "user gesture" mean here? It seems that only click or keydown event, but not mousemove event. –  Oct 06 '16 at 15:21
11

Other answers already describe how you can go fullscreen in a more or less browser-independent way. However, problem of needing user interaction remains.


You cannot force your webpage to display in fullscreen mode for security reasons. User interaction is required for that.

Also, browser leaves fullscreen mode whenever user goes to another page, even on the same website, and (s)he will have to perform some "user interaction" on every page to go back into fullscreen mode.
This is the reason why your website has to be a single page if you want it to be fullscreen.


That is what I suggest: Use a single splash page that has a hidden iFrame on it.

Whenever user clicks anywhere or presses any key, just set this iFrame to be fullscreen and show it. When you receive an event on leaving fullscreen mode, hide iFrame again to show splash.

Links open in the same frame by default, so you will stay in fullscreen mode until user explicitly leaves it or some links opens in a new tab.

Here is an example that works in Chrome:
(See it in action. Use other answers to make it browser-independent.)

<html>
<head>
    <script language="jscript">
        function goFullscreen() {
            // Must be called as a result of user interaction to work
            mf = document.getElementById("main_frame");
            mf.webkitRequestFullscreen();
            mf.style.display="";
        }
        function fullscreenChanged() {
            if (document.webkitFullscreenElement == null) {
                mf = document.getElementById("main_frame");
                mf.style.display="none";
            }
        }
        document.onwebkitfullscreenchange = fullscreenChanged;
        document.documentElement.onclick = goFullscreen;
        document.onkeydown = goFullscreen;
    </script>
</head>
<body style="margin:0">
    <H1>Click anywhere or press any key to browse <u>Python documentation</u> in fullscreen.</H1>
    <iframe id="main_frame" src="https://docs.python.org" style="width:100%;height:100%;border:none;display:none"></iframe>
</body>
</html>

Note, however, that websites often disallow embedding, e.g. being displayed inside an iframe. The example initially used W3Schools website instead of Python docs, but they set 'X-Frame-Options' header to 'sameorigin' (disallow cross-site embedding) and it stopped working.


P.S. I like the idea of simulating full-blown OS in browser, and it's even better in fullscreen! :) Change your OS!
Also, I am not a web developer. Just thought this question would be interesting to investigate.

EvgEnZh
  • 699
  • 8
  • 13
  • 1
    Thank you for adding an actual USEFUL answer - unlike other nay-sayers, you provided a working solution and not useless rants. Bravo. – RomanM Mar 13 '19 at 22:23
4

You might be able to use requestFullScreen() methods as described at https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode.

Note : This still requires user input - but avoid usage of flash.

function toggleFullScreen() {
  if (!document.fullscreenElement &&    // alternative standard method
      !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) {  // current working methods
    if (document.documentElement.requestFullscreen) {
      document.documentElement.requestFullscreen();
    } else if (document.documentElement.msRequestFullscreen) {
      document.documentElement.msRequestFullscreen();
    } else if (document.documentElement.mozRequestFullScreen) {
      document.documentElement.mozRequestFullScreen();
    } else if (document.documentElement.webkitRequestFullscreen) {
      document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
    }
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
      document.webkitExitFullscreen();
    }
  }
}

toggleFullScreen();

This allows the page to activate full screen, and could possibly be activated via page load on a javascript page. However, there is no support for older browsers.

Kami
  • 19,134
  • 4
  • 51
  • 63
  • 9
    You can't call those functions without user interaction. Which is the reason the OP posted this question in the first place. The only answer to his question, to initialize fullscreen without user interaction, is a _"no can do"_. – Cerbrus Mar 26 '15 at 15:13
  • 5
    `Failed to execute 'requestFullScreen' on 'Element': API can only be initiated by a user gesture.` – Tom Mar 26 '15 at 15:15
  • this function can execute when you have an user gesture like onClick, but you can not force from your code – TuanHV Jul 01 '20 at 04:34
  • `Element.ALLOW_KEYBOARD_INPUT` what is this for? – Birat Feb 10 '23 at 11:43
  • @Birat - It allows for keyboard interaction while in full screen mode for some browsers (WebKit is Safari,chrome etc). see https://hacks.mozilla.org/2012/01/using-the-fullscreen-api-in-web-browsers/ – Kami Feb 13 '23 at 16:25
2

When I needed to do something similar I used a splash screen. The button to continue into full screen mode requested it as an attribute of a JS pop:

 onClick="window.open('pageName.html', 'test', 'fullscreen=yes')"

This is not fullproof, but worked better than any other methods I found. You likely won't be able to do this without user interaction, so using something like a splashscreen allows you to minimize the intrusion to something more commonly accepted.

Nicholas
  • 1,974
  • 4
  • 20
  • 46
  • 1
    My browser blocks popups like that by default. – Cerbrus Mar 26 '15 at 15:20
  • @Cerbrus That's a good point. I had set this up several years ago, before popup blockers were so common. – Nicholas Mar 26 '15 at 15:56
  • 1
    If you're already using an onclick event... why not just set the current webpage to fullscreen? – Neil Nov 13 '17 at 03:02
  • @Neil It will open `pageName.html` in a new window which means current page can be anything, say "xyz.html". Also, there is a possibility that `pageName.html` doesn't have an `onClick` event. – Chetan Goyal Mar 03 '23 at 11:11
0

This is not exactly what the OP was looking for, but in my particular situation, I found a combination of kiosk mode and some dynamic styling to work.

I was looking to start a browser with a particular URL and have it start in full screen mode. The use case is little to no user interaction on a terminal in a manufacturing plant with a status screen. Following power up, it needs to display only the status without interaction (i.e. display none of configuration UI elements unless the user interacts).

Perhaps, not realistic on a complex site, but my use was a particular "pane" (div in this case) element. To focus on the particular div, I hid the other divs, and set styles accordingly. If there happens to be user interaction to change from and to fullscreen, I (1) use the regular myElement.*requestFullScreen() document.*exitFullscreen() calls, (2) show/hide the other divs, and (3) switch between the classes below:

.right-pane-fullscreen
{
    margin-left: 0px;
}

.right-pane-normal
{
    margin-left: 225px;
}

Related discussion on how to use kiosk mode in Chrome (be aware that other Chrome processes running prior to launching kiosk mode can prevent this from working) Opening Chrome browser in full window or kiosk mode on windows 7

pigeon
  • 151
  • 9