There is actually a fully functional example on MDN:
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode#Toggling_fullscreen_mode
Quote:
Toggling fullscreen mode
This code is called when the user hits the Enter key, as seen above.
function toggleFullScreen() {
if (!document.fullscreenElement && // alternative standard method
!document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) { // current working methods
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
} else if (document.documentElement.msRequestFullscreen) {
document.documentElement.msRequestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
}
This starts by looking at the value of the fullscreenElement attribute
on the document (checking it prefixed with both moz, ms, and webkit).
If it's null, the document is currently in windowed mode, so we need
to switch to fullscreen mode. Switching to fullscreen mode is done by
calling either element.mozRequestFullScreen() msRequestFullscreen()or
webkitRequestFullscreen(), depending on which is available.
If fullscreen mode is already active (fullscreenElement is non-null),
we call document.mozCancelFullScreen(), msExitFullscreen or
webkitExitFullscreen(), again depending on which browser is in use.