javascript - prevent enter key from closing alert - Stack Overflow

Basically, I want to make sure that a user has to manually close a javascript alert, and that the user

Basically, I want to make sure that a user has to manually close a javascript alert, and that the user can't simply quit out of the alert by pressing enter.

(This sounds malicious, but in the application you press enter a lot and I need to make sure they don't miss the important information the alert gives them).

I have tried setting the document.onkeydown to no avail:

document.onkeydown = disabled;

and then in the disabled function

function disabled(event) {
    if (event.keyCode==13) {
        event.preventDefault();
    }
}

is there a way to acplish this without having to switch over to using modals?

Basically, I want to make sure that a user has to manually close a javascript alert, and that the user can't simply quit out of the alert by pressing enter.

(This sounds malicious, but in the application you press enter a lot and I need to make sure they don't miss the important information the alert gives them).

I have tried setting the document.onkeydown to no avail:

document.onkeydown = disabled;

and then in the disabled function

function disabled(event) {
    if (event.keyCode==13) {
        event.preventDefault();
    }
}

is there a way to acplish this without having to switch over to using modals?

Share Improve this question asked Jun 25, 2015 at 16:49 pasquerspasquers 7921 gold badge9 silver badges29 bronze badges 5
  • 6 Change the call to alert() to some custom modal window implementation. – Halcyon Commented Jun 25, 2015 at 16:51
  • Slight nitpick... Pressing enter is a manual action, the user is "manually closing the alert". – David Commented Jun 25, 2015 at 16:53
  • @Halcyon: The end of the question: "is there a way to acplish this without having to switch over to using modals?" (my emphasis) – T.J. Crowder Commented Jun 25, 2015 at 16:55
  • @T.J.Crowder I'm not sure why you made that ment. window.alert() is a modal. I guess the emphasis is on custom. If you're unsure of the term, see: en.wikipedia/wiki/Modal_window – Halcyon Commented Jun 25, 2015 at 16:57
  • @Halcyon: To me it's clear that the OP's talking about a custom modal window implementation there. But it's not a big deal. Re your edit: Of course I'm sure of the term. I also have a decent grasp of web programming vernacular. Guess what "modal" means there, 99 out of 100 times. – T.J. Crowder Commented Jun 25, 2015 at 16:59
Add a ment  | 

2 Answers 2

Reset to default 8

is there a way to acplish this without having to switch over to using modals?

No. The alert is outside the document and you can't add any event handling to it. The path you've already identified (switching to a "modal" element) is the way you'll have to go. It also has the advantage you can improve styling, prevent the browser from suppressing the alerts, etc. There's some refactoring required to handle the async pletion, of course.

A kludgy solution would be for the alert to always be in the foreground so they will see it, but to grey out the alert by having windows focus on some other object, so that the user's 'enter' will go to that instead.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745170428a4614892.html

相关推荐

  • javascript - prevent enter key from closing alert - Stack Overflow

    Basically, I want to make sure that a user has to manually close a javascript alert, and that the user

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信