Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript confirm dialog

I want to add a confirm dialog to a delete button to ask the user whether it is ok or not deleting the selected item.

If not, nothing should happend, else a url should be executed.

I know how to realize this via some Javascript code but I am looking for a solution that has less code. I mean e.g. :

<a href="mysite.de/xy/delete" onClick="...">Delete</a>

Is it possible to put the whole functionality in the onClick element without having some extra Javascript in the header?

like image 252
UpCat Avatar asked Nov 27 '10 15:11

UpCat


People also ask

How do you use confirm in JavaScript?

Javascript | Window confirm() Method The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed.

Should I use confirm JavaScript?

The confirm JavaScript box forces the browser to read the message. By doing so, it takes the focus off the current window. This function should not be overused, as it restricts the user from reaching other page parts until the box is closed.

What is confirm box in JavaScript?

A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

How can write confirm box in JavaScript?

You can create a JavaScript confirmation box that offers yes and no options by using the confirm() method. The confirm() method will display a dialog box with a custom message that you can specify as its argument.


2 Answers

You can return the confirm() (which returns true/false), like this:

<a href="mysite.de/xy/delete" onClick="return confirm('You sure??');">Delete</a>

You can test it here

like image 154
Nick Craver Avatar answered Oct 21 '22 00:10

Nick Craver


Better (though far from ideal!): turn it around. Don't let the link do anything, unless you got JavaScript:

<a href="#" 
  onclick="if confirm('Sure?') { window.location='http://mysite.de/xy/delete';}">
    Click to delete
</a>

This at least prevents the link to work without JavaScript. This also reduces the risk of the link accidentally being crawled by Google, or even by some local plugin. (Image if you had a plugin that would try to load/show as thumbnail) the target page on hover of a link!)

Still, this solution is not ideal. You will actually browse to the url, and the url might show up in the history because of that. You could actually delete Bob, create a new Bob, and then delete that one by accident by just clicking 'back' in the browser!

A better option would be to use JavaScript or a form to post the desired action. You can make a request to the server with the POST method, or arguably better, the DELETE method. That should also prevent the urls from being indexed.

like image 42
GolezTrol Avatar answered Oct 21 '22 01:10

GolezTrol