如何通过JavaScript使用F11键事件使浏览器全屏显示

如何通过JavaScript使用F11键事件使浏览器全屏显示,javascript,browser,screen,fullscreen,keyevent,Javascript,Browser,Screen,Fullscreen,Keyevent,我想让我的浏览器全屏显示。与执行F11关键事件时相同。我发现了一些例子,比如 function maxwin() { var wscript = new ActiveXObject("WScript.Shell"); if (wscript!=null) { wscript.SendKeys("{F11}"); } } 这在mozilla或任何其他最新浏览器上都不起作用。如果有办法解决这个问题,请告诉我 谢谢。(提前。)如果没有本机代码或浏览器扩展,则

我想让我的浏览器全屏显示。与执行F11关键事件时相同。我发现了一些例子,比如

function maxwin() {
    var wscript = new ActiveXObject("WScript.Shell");
    if (wscript!=null) {
         wscript.SendKeys("{F11}");
    }
}
这在mozilla或任何其他最新浏览器上都不起作用。如果有办法解决这个问题,请告诉我


谢谢。(提前。)

如果没有本机代码或浏览器扩展,则不可能。ActiveXObject仅存在于IE浏览器中。

现在有可能(至少在safari 5、chrome 16的webkit浏览器中)使用

 webkitEnterFullscreen()
Firefox10也可以使用

不知道i-e在本主题中做了什么

请使用此代码

var el = document.documentElement
, rfs = // for newer Webkit and Firefox
       el.requestFullScreen
    || el.webkitRequestFullScreen
    || el.mozRequestFullScreen
    || el.msRequestFullScreen
;
if(typeof rfs!="undefined" && rfs){
  rfs.call(el);
} else if(typeof window.ActiveXObject!="undefined"){
  // for Internet Explorer
  var wscript = new ActiveXObject("WScript.Shell");
  if (wscript!=null) {
     wscript.SendKeys("{F11}");
  }
}
资料来源:


在铬合金、FF10以上、IE 8以上、Safari 5上工作并测试

这在最新版本的Chrome、Firefox和IE(11)中是可能的

按照Zuul on的指示,我编辑了他的代码,包括IE11和全屏显示页面上任何选择元素的选项

JS:

HTML:


对于IE来说,“!important”似乎是必要的,我想知道自2011年9月21日以来有多少变化?Facebook now(2012年4月9日)拥有使其弹出窗口全屏显示的功能(至少肯定是在Chrome中),我仍然肯定在使用浏览器(而不是flash),因为我可以“检查元素”和做其他Chrome事情..非常正确:正如@Treby指出的,现在出现了全屏API,因此,这可能不再有效。请注意,尽管全屏API和F11是[同样的事情)。这个答案现在已经过时了-关于使用全屏API的解决方案,请参阅此线程中的其他答案。我已经尝试过其他解决方案。这个最佳答案100%有效。请注意
requestFullScreen
afaik中的大写字母
S
已被弃用(但可能仍在firefox中)而且应该有一个小的
s
requestFullscreen
。这花了我一段时间才弄明白谢谢!我正在运行一个blazor应用程序,这个代码片段解决了这个问题。Shot bru。@Treby:如果我们想退出,那么代码是什么呢?注意
requestFullscreen
afaik中的大写字母
s
(但可能仍在firefox中),应该有一个小的
s
requestFullscreen
。这花了我一段时间才弄明白
function toggleFullScreen(elem) {
    // ## The below if statement seems to work better ## if ((document.fullScreenElement && document.fullScreenElement !== null) || (document.msfullscreenElement && document.msfullscreenElement !== null) || (!document.mozFullScreen && !document.webkitIsFullScreen)) {
    if ((document.fullScreenElement !== undefined && document.fullScreenElement === null) || (document.msFullscreenElement !== undefined && document.msFullscreenElement === null) || (document.mozFullScreen !== undefined && !document.mozFullScreen) || (document.webkitIsFullScreen !== undefined && !document.webkitIsFullScreen)) {
        if (elem.requestFullScreen) {
            elem.requestFullScreen();
        } else if (elem.mozRequestFullScreen) {
            elem.mozRequestFullScreen();
        } else if (elem.webkitRequestFullScreen) {
            elem.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
        } else if (elem.msRequestFullscreen) {
            elem.msRequestFullscreen();
        }
    } else {
        if (document.cancelFullScreen) {
            document.cancelFullScreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
        } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
        }
    }
}
<input type="button" value="click to toggle fullscreen" onclick="toggleFullScreen(document.body)">
*:fullscreen
*:-ms-fullscreen,
*:-webkit-full-screen,
*:-moz-full-screen {
   overflow: auto !important;
}