Exit from fullscreen mode on click in JavaScript
Question:
How to exit from fullscreen mode on click in JavaScript? Answer:
const exitBtn = document.getElementById('exit-btn');
exitBtn.addEventListener('click', ()=>{
if (document.fullscreenElement) {
document.exitFullscreen();
}
});
Description:
To exit from a full-screen mode using vanilla JavaScript use the exitFullscreen method. The Document method exitFullscreen() requests that the element on this document which is currently being presented in full-screen mode be taken out of full-screen mode, restoring the previous state of the screen.
Before calling exitFullscreen check if there is any element in full-screen mode to avoid errors using if (document.fullscreenElement)
Also, note that you need to call exitFullscreen on the document and not on the element .
Reference:
exitFullscreen reference
Share "How to exit from fullscreen mode on click in JavaScript?"
Related snippets:
Tags:
exit, fullscreen, exitFullscreen, fullscreenElement, Technical term:
Exit from fullscreen mode on click in JavaScript