How to disable Esc button event on alert?
The issue with alert()
is that it will always return undefined
, giving you no indication of what was pressed. Instead, you can use confirm()
which will allow you to display a similar sort of popup.
However, the advantage of confirm
is that it returns a true
if you clicked "OK" on the popup and false
if you pressed esc or "cancel".
Thus, using confirm
you can use a do-while
loop to keep popping up your popup until the user has pressed "OK" (or pressed the enter key)
See example below:
let res;
do {
res = confirm("Popup");
} while(!res);
Using the native confirm/alert box isn't a good user experience as it freezes the UI thread. If you can avoid it, I suggest using the DOM to open popups/modals built through HTML. This can easily be done by using a library.