Why [removed] = launchFullscreen(document.documentElement); not work?

后端 未结 3 585
礼貌的吻别
礼貌的吻别 2020-12-22 12:09

I use launchFullscreen() function for get page full screen. It\'s work perfect with button onClick.But It doesn\'t work with window.onload

相关标签:
3条回答
  • 2020-12-22 12:33

    Try this:

    window.onload = function() {
    launchFullscreen(document.documentElement);
    }
    
    0 讨论(0)
  • 2020-12-22 12:39

    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.

    0 讨论(0)
  • 2020-12-22 12:46

    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.

    0 讨论(0)
提交回复
热议问题