Tobiq
Tobiq

Reputation: 2657

I can't exit full screen

I used the Full Screen API to set the document body to fullscreen.

Now, all calls for document.exitFullScreen (with the appropriate prefixes) result in the error:

Uncaught TypeError: Illegal invocation
at HTMLDivElement.fs.onclick

The function neither works in an event handler, or in the normal JS flow.

What is the problem, and how do I fix this? There is so little documentation, and I have not found anyone else with the error.

Upvotes: 1

Views: 1103

Answers (2)

klues
klues

Reputation: 937

I had the same error on calling document.exitFullscreen() directly, after showing a YouTube Video in an iframe in fullscreen.

For me this is working:

let exitFullscreen = document.exitFullscreen || document.mozCancelFullScreen || document.webkitExitFullscreen || document.msExitFullscreen;
if (exitFullscreen) {
    exitFullscreen.bind(document)();
}

It's necessary to bind() the function to document. Otherwise the context is probably the iframe, where invoking the function is illegal.

Upvotes: 3

Dorival
Dorival

Reputation: 689

Entering or exiting FullScreen is an invasive operation, and therefore, should only be triggered directly by an user action (FullScreen API). Anything else will cause this error, for security reasons.

This question discussed how to invoke FullScreen without bumping in this Illegal action. You can code a solution to exit fullscreen based on their answer: Getting fullscreen mode to my browser using jquery

The answer to that question has a solution without jQuery, which is cool.

Upvotes: 0

Related Questions