javascripthtmlcssfirefoxfullscreen

How to initialize fullscreen without user interaction?


I want to create a web app and need help with fullscreen when the page loads, without user interaction.

I have something like this in the click function, which works correctly; however, I want the function to run when the page starts.

button.addEventListener("click", function() {
  var el = document.documentElement
  var rfs = el.requestFullScreen ||
            el.webkitRequestFullScreen ||
            el.mozRequestFullScreen;

  rfs.call(el);
});

Solution

  • That is not possible.

    I ran the following snippet in my browser console:

    var e = document.getElementById('answers');
    (e.webkitRequestFullScreen || e.mozRequestFullScreen).apply(e);
    

    Chrome told me:

    Failed to execute 'requestFullScreen' on 'Element': API can only be initiated by a user gesture.

    Firefox told me:

    Request for full-screen was denied because Element.mozRequestFullScreen() was not called from inside a short running user-generated event handler.

    That is a restriction put in place to prevent abuse, similar to that on window.open (see this or this question for example).