最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prevent esc from exiting fullscreen mode - Stack Overflow

programmeradmin0浏览0评论

My webpage converted to full screen mode using this following code.

    var element = document.getElementById("b");


    if (element.mozRequestFullScreen) {

      element.mozRequestFullScreen();
    } else if (element.webkitRequestFullScreen) {

      element.webkitRequestFullScreen();
   }

I want to prevent exit from fullscreen mode by clicking esc

I tried this following codes but not working.

document.onkeydown = function (evt) {
            alert("1");
    if (evt.keyCode == 27) evt.preventDefault();
}

If i press escape it first exit from full screen and from next press the above function will work and alert "1".

I tried the following codes also but no benefit

  function keyUp(){
    alert("sdd");
document.querySelector("#start").addEventListener("keydown",function(e){
    var charCode = e.charCode || e.keyCode || e.which;
    if (charCode == 27){

        return false;
    }
});

}

and

document.onkeydown = function (e) {
        if(e.which == 27){
                return false;
        }
}

Anybody know how to prevent exiting from full screen mode. Or how to convert webpage to fullscreen mode using f11 key programatically?

My webpage converted to full screen mode using this following code.

    var element = document.getElementById("b");


    if (element.mozRequestFullScreen) {

      element.mozRequestFullScreen();
    } else if (element.webkitRequestFullScreen) {

      element.webkitRequestFullScreen();
   }

I want to prevent exit from fullscreen mode by clicking esc

I tried this following codes but not working.

document.onkeydown = function (evt) {
            alert("1");
    if (evt.keyCode == 27) evt.preventDefault();
}

If i press escape it first exit from full screen and from next press the above function will work and alert "1".

I tried the following codes also but no benefit

  function keyUp(){
    alert("sdd");
document.querySelector("#start").addEventListener("keydown",function(e){
    var charCode = e.charCode || e.keyCode || e.which;
    if (charCode == 27){

        return false;
    }
});

}

and

document.onkeydown = function (e) {
        if(e.which == 27){
                return false;
        }
}

Anybody know how to prevent exiting from full screen mode. Or how to convert webpage to fullscreen mode using f11 key programatically?

Share Improve this question edited Apr 3, 2020 at 10:53 ADyson 61.9k15 gold badges73 silver badges88 bronze badges asked Jan 22, 2014 at 9:39 ParvathyParvathy 2,4554 gold badges26 silver badges39 bronze badges 9
  • 7 Why do you want to do this? This would be annoying and I wouldn't visit your site again. – Lee Taylor Commented Jan 22, 2014 at 9:43
  • I certainly hope browser are not stupid and browser hijacking like that doesn't work. Otherwise it would be an epic failure in my eyes from the side of browser vendors / w3c. – PeeHaa Commented Jan 22, 2014 at 9:43
  • 1 And that is solved programmatically from the browser? I hope not... – PeeHaa Commented Jan 22, 2014 at 9:52
  • 1 I know chrome has a --kiosk flag – PeeHaa Commented Jan 22, 2014 at 9:55
  • 1 I've got a remote desktop running full screen and I'm running Emacs in a remote window. I use ESC all the time as part of Emacs control sequences, and every time I hit ESC it drops me out of full screen. This feature cripples full screen for a remote desktop. – Brent Baccala Commented Aug 26, 2020 at 18:58
 |  Show 4 more comments

3 Answers 3

Reset to default 13

Any browsers complying to the standards will not (and should not) let you block this. Quite simply because it would be very intrusive and will confuse the users, who expect to always have a clear "panic button" to escape a screen.

Here are the api specifications:
Mozilla Developer Network
W3C

That's impossible from pure js. The API is designed to allow the user to exit the fullscreen mode at any time.

The only thing cou can try is to use the fullscreenchange event to detect when the fullscreen is disabled, and then enable it again. But the browser will probably alert the user and he will always be able to disallow your website to use fullscreen.

I don't encourage you to do it. That's really a bad idea. I would probably ragequit (and never return) a website which tries that.

You can use navigator.keyboard.lock(); to prevent the escape key from exiting full screen mode (or from unlocking the pointer for that matter).

Though it's only supported in Chromium based browsers and still allows the user to exit full screen by holding down the escape key.

Check out the Keyboard API MDN Web Docs.

发布评论

评论列表(0)

  1. 暂无评论