Hello!I would like to add message verifcation in JS to ask the user "Are you sure you want to perform this action?" before it's making the action (in my case - removing some user details in PHP)how I can do that?

@felgall ;I have heard you say these are only for debugging purposes. Is there an alternative that I have overlooked?

davidfregoli
—
2014-01-09T16:03:24Z —
#3

confirm, alert, prompt and the likes block the browser and therefore are considered bad practice by many (not to demonize, I use them myself sometimes, but it's nice to be aware of it and follow better standars if the project allows it). of course they're super good for debugging/prototyping.the alternative is simply an html modal/popup.obviously it can't be used the same way since it's not-blocking and "asyncronous" so you'd have to refactor using callbacks, or, if you're super cool, promises

Pullo
—
2014-01-09T16:16:50Z —
#4

Thanks. So what is a better way of confirming if a user really wants to perform an action (for example to ensure that they didn't click on 'delete' by mistake)?

Edit:

I see you updated your answer to include HTML modal. Could you point me to an example of what you mean?

system
—
2014-01-09T16:28:16Z —
#5

You can omit putting a submit type button for the form. Then you use a normal button type instead, display a dialog on click, and only .submit() the form if the dialog returns the sure value.

If you do things smartly you will keep a single one in the DOM and change its message and buttons labels as you need, while binding the appropriate function callbacks to handle the pressing of the buttons.

If you do things smartly you will keep a single one in the DOM and change its message and buttons labels as you need, while binding the appropriate function callbacks to handle the pressing of the buttons.

That's definitely the right way to do it.

The confirm dialog is considered by some browsers to be for debugging purposes and so may display an extra checkbox to aid in debugging - for example by making it easy to turn JavaScript off.

Stomme_poes
—
2014-01-09T20:32:48Z —
#8

I use built-in alerts and the such quite often, because making modals accessible, especially for screen readers, is still eating the hottest chicken vindaloo and pooping it out in an endless trial of fire and dirty poophacks.

Some day the modal dialog will be built into the browser where it belongs, instead of the long string of cringeworthy a11y-hacks and manual keyboard focus it is now.

That said, I'm always pleased when I run across some plugin thingie where someone took the time to do all the hacks for me. I'm looking for a lighbox-container for dialogs and popups and have seen a few now with keyboard-traps, default-escapes, multi-language keybindings (even control keys often aren't the same code across different-language keyboards, so even listening for ENTER can be complicated depending on who you're supporting) and the like.

But I'm still of the opinion that developers on the front shouldn't have to be building these wheels over and over again.

Also, the fact that everything stops until that confirm/alert/whatever is dealt with, is sometimes exactly what you want, which should also help you decide which type you'd want to use.