hendraspt
hendraspt

Reputation: 969

How to change onclick become onload?

I want to make the page fullscreen automatically without the need to click anything. I've tried to change this <body onclick="toggleFullScreen()"> into <body onload="toggleFullScreen()">. but nothing happens; it doesn't work. :(

This is the javascript..

function errorHandler() {
   alert('mozfullscreenerror');
}
document.documentElement.addEventListener('mozfullscreenerror', errorHandler, false);

// toggle full screen

function toggleFullScreen() {
  if (!document.fullscreenElement &&    // alternative standard method
      !document.mozFullScreenElement && !document.webkitFullscreenElement) {  // current working methods
    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();
    }
  }
}

Upvotes: 1

Views: 356

Answers (1)

eusoj
eusoj

Reputation: 374

You can't execute any RequestFullScreen without a user gesture in any browser.

By the way your full screen function can be like this :

  function fullScreen() {
    var el = document.documentElement;
    var rfs = el.requestFullScreen
              || el.webkitRequestFullScreen
              || el.mozRequestFullScreen;
    rfs.call(el);
  }

Upvotes: 1

Related Questions