简体   繁体   English

为什么 window.onload = launchFullscreen(document.documentElement); 不行?

[英]Why window.onload = launchFullscreen(document.documentElement); not work?

I use launchFullscreen() function for get page full screen.我使用launchFullscreen()函数来获取页面全屏。 It's work perfect with button onClick .But It doesn't work with window.onload .它与按钮onClick完美配合。但它不适用于window.onload Are there are any way to call that function from onload.有什么办法可以从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();
    }
}

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如果满足以下任一条件,则将任务排队以触发名为 fullscreenerror 的事件,其气泡属性在上下文对象的节点文档上设置为 true,然后终止这些步骤

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. launchFullscreen ,正如 Theo 的回答中所指出的,您正在立即调用launchFullscreen并尝试使用其返回值(不是函数)作为加载事件处理程序。 In this case, it makes no difference though.在这种情况下,它没有任何区别。

Try this:尝试这个:

window.onload = function() {
launchFullscreen(document.documentElement);
}

I work around the "must be user action" by binding it to a click event on the HTML element.我通过将“必须是用户操作”绑定到 HTML 元素上的单击事件来解决“必须是用户操作”的问题。

$('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.即使您忘记了,第一次单击也会使页面全屏显示。

ps: Yes, you can easily do this without using jQuery by using proper getElement... functions. ps:是的,通过使用适当的 getElement... 函数,您可以在不使用 jQuery 的情况下轻松完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM