0

I use launchFullscreen() function for get page full screen. It's work perfect with button onClick.But It doesn't work with window.onload. Are there are any way to call that function from onload.

window.onload = launchFullscreen(document.documentElement);

function launchFullscreen(element) {
    if(element.requestFullscreen) {
        element.requestFullscreen();
    } else if(element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
    } else if(element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen();
    } else if(element.msRequestFullscreen) {
        element.msRequestFullscreen();
    }
}
lukas.pukenis
  • 13,057
  • 12
  • 47
  • 81
Asith
  • 11
  • 1
  • 1
  • Have you tried using JQuery and put it into `$(document).ready(function(){ launchFullscreen(document.documentElement);});`? – mortb Apr 30 '14 at 08:40

3 Answers3

2

See the specification:

If any of the following conditions are true, queue a task to fire an event named fullscreenerror with its bubbles attribute set to true on the context object's node document, and then terminate these steps

This algorithm is not allowed to show a pop-up.

Full screen mode may only be triggered at times when it is allowed to show a popup.

You may only show a popup in response to a user event.

A click is a user event.

The document loading is not.

There is no way around this.


An an aside, as pointed out in Theo's answer, you are calling launchFullscreen immediately and trying to use its return value (not a function) as the load event handler. In this case, it makes no difference though.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • window.onload = function launchFullscreen(element) { if(element.requestFullscreen) { element.requestFullscreen(); } else if(element.mozRequestFullScreen) { element.mozRequestFullScreen(); } else if(element.webkitRequestFullscreen) { element.webkitRequestFullscreen(); } else if(element.msRequestFullscreen) { element.msRequestFullscreen(); } } – Asith Apr 30 '14 at 09:38
  • @Asith — You are still trying to call it onload. onload is still not an event triggered by the user. popups are still banned. fullscreen is therefore also still banned. – Quentin Apr 30 '14 at 09:41
0

Try this:

window.onload = function() {
launchFullscreen(document.documentElement);
}
Theo
  • 150
  • 11
  • Hi Asith, I figure it is not possible the way you would like, see this item about the fullscreen onload issue: http://stackoverflow.com/questions/10352236/full-screen-browser-window-on-load-document – Theo May 01 '14 at 09:53
0

I work around the "must be user action" by binding it to a click event on the HTML element.

$('html').click( function() {
  if(!document.fullscreenElement){
    $('html')[0].requestFullscreen();
  }
});

You just need to remember to touch something once it is loaded. Even if you forgot, the first click will put the page to full screen.

p.s.: Yes, you can easily do this without using jQuery by using proper getElement... functions.

Nelson
  • 2,040
  • 17
  • 23