Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 423
pasquers Avatar asked Jun 25 '15 16:06

pasquers


1 Answers

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.

like image 57
T.J. Crowder Avatar answered Sep 21 '22 21:09

T.J. Crowder