Failed to execute 'requestFullScreen' in 'Element': API can only be triggered by user gesture

I want my HTML5 game to start in full screen mode at startup. When I do this with the onclick button, it goes into full screen mode. But when I use window.onload to make it work in fullscreen onload mode, it responds to the console. Failed to execute "requestFullScreen" in "Element": API can only be initiated by user gesture. I use chrome. Is there a workaround?

My code is:

<!DOCTYPE html> <html> <head> <script> window.onload = openfullscreen(); function launchIntoFullscreen(element) { if(element.requestFullscreen) { element.requestFullscreen(); } else if(element.mozRequestFullScreen) { element.mozRequestFullScreen(); } else if(element.webkitRequestFullscreen) { element.webkitRequestFullscreen(); } else if(element.msRequestFullscreen) { element.msRequestFullscreen(); } } function openfullscreen() { launchIntoFullscreen(document.documentElement); } function exitFullscreen() { if(document.exitFullscreen) { document.exitFullscreen(); } else if(document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if(document.webkitExitFullscreen) { document.webkitExitFullscreen(); } window.close(); } function fix() { var screenwidth = screen.width; var screenhei = screen.height; document.getElementById('ifam').width = screenwidth; document.getElementById('ifam').height = screenhei; } </script> <style> #ifam { position:fixed; left:0%; top:0%; z-index:-1; } #fullscreen { position:fixed; left:0%; top:0%; z-index:1; } </style> </head> <body onload="fix()"> <div id="fullscreen"> <button onclick="openfullscreen()">Open</button> <button onclick="exitFullscreen()">Exit</button> </div> <iframe id="ifam" src="lchosser.html"></iframe> </body> </html> 
+5
source share
1 answer

Any javascript api is intentionally made this way, I think. Imagine if you go to a site and it automatically turns on in full screen mode, if you do nothing at all. You would be exposed to a world of annoying pop-ups and other things that you even intend to open. Therefore, any such script requires user interaction. Switching to full-screen mode without user interaction is very similar to malicious intent. Example, if such a site opens in full-screen mode: "Virtual Desktop", someone can greatly confuse what happened to their desktop.

If you still want to try a partial workaround, you can take a look at the following answer:

Partial WorkAround

+3
source

All Articles