pasquers pasquers - 4 months ago 16
Javascript Question

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?

Answer

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.

Comments