Is there any way to enable browser fullscreen with Javascript?

22

I notice that the main browsers are in full screen mode when pressing the F11 key, is this a functionality of the browser itself or is it possible to activate this through some Javascript code?

If you can not do it via code, just open a new window with only the page. No browser buttons, and with the address bar locked etc.

    
asked by anonymous 10.01.2014 / 19:38

2 answers

15

There is a way to simulate the use of the F11 key , but it can not be automated. p>

As this is a feature that only the user can control, it will have to click on a button ( for example ) to activate full screen mode.

  • Example Switch

    This example allows you to switch between full screen and normal browser window by clicking on the same element.

    Action button

    <input type="button" value="clique para alternar" onclick="toggleFullScreen()">
    

    Function

    function toggleFullScreen() {
      if ((document.fullScreenElement && document.fullScreenElement !== null) ||    
       (!document.mozFullScreen && !document.webkitIsFullScreen)) {
        if (document.documentElement.requestFullScreen) {  
          document.documentElement.requestFullScreen();  
        } else if (document.documentElement.mozRequestFullScreen) {  
          document.documentElement.mozRequestFullScreen();  
        } else if (document.documentElement.webkitRequestFullScreen) {  
          document.documentElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);  
        }  
      } else {  
        if (document.cancelFullScreen) {  
          document.cancelFullScreen();  
        } else if (document.mozCancelFullScreen) {  
          document.mozCancelFullScreen();  
        } else if (document.webkitCancelFullScreen) {  
          document.webkitCancelFullScreen();  
        }  
      }  
    } 
    
  • Switch to Full Screen Example

    This example allows you to activate the full screen mode without making a switch, ie the user switches to full screen but to return to the normal screen you must use the F11 key:

    Action button

    <input type="button" value="clique para ativar tela cheia" onclick="requestFullScreen()">
    

    Function

    function requestFullScreen() {
    
      var el = document.body;
    
      // Supports most browsers and their versions.
      var requestMethod = el.requestFullScreen || el.webkitRequestFullScreen 
      || el.mozRequestFullScreen || el.msRequestFullScreen;
    
      if (requestMethod) {
    
        // Native full screen.
        requestMethod.call(el);
    
      } else if (typeof window.ActiveXObject !== "undefined") {
    
        // Older IE.
        var wscript = new ActiveXObject("WScript.Shell");
    
        if (wscript !== null) {
          wscript.SendKeys("{F11}");
        }
      }
    }
    

Sources and useful information I found on this subject:

Response Credits for the @Zuul user in this answer in the original StackOverflow.

    
10.01.2014 / 19:45
8

You can enable fullscreen via JavaScript at certain browsers . You can even place a single element in fullscreen (like a video), or the entire document.

Example for the entire document (extracted from MDN ):

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 API is not yet fully standardized. A while ago, there was a restriction that Full Screen mode would need to be triggered by user intervention (such as a click on something). I'm not sure if the constraint still exists, I did not see any reference in the current MDN article (but there's some clues that this limitation already existed in Firefox).

    
10.01.2014 / 19:42