简体   繁体   中英

prevent enter key from closing alert

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 accomplish this without having to switch over to using modals?

is there a way to accomplish 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 completion, of course.

一个kludgy解决方案是让警报始终在前台,这样他们就可以看到它,但是通过让窗口聚焦于其他一些对象来使警报变灰,这样用户的“输入”就会转向它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM