Spiga

Javascript Confirm Delete

by Gabi Solomon

On all my projects there was at least one delete buton or a diferent action that would be irevocable. And many times during the developing phase i forgot to put a method of preventing clicking that link by mistake. Of course you can do a confirmation page from php that would be better since it will not depend on the user having javascript activated, but during the developing phase it is better to put a simple javascript confirm even if you will develop a php confirmation page. This will save some load of the server if the user has javascript activated, and save the user some time. Also it is best to always have a confirmation method since many times the user is in a hurry to click and might click the worng button.

There are 2 methods of doing a javascript confirm.

Using a separate function

HTML:
  1. function confirmDelete(delUrl) {
  2.   if (confirm("Are you sure you want to delete")) {
  3.     document.location = delUrl;
  4.   }
  5. }
  6. </script>
  7.  
  8. <a href="javascript:confirmDelete('delete.page?id=1')">Delete</a>

Confirming directly in the link

HTML:
  1. <a href="delete.page?id=1" onclick="return confirm('Are you sure you want to delete?')">Delete</a>

I have gotten my slef used to use the last method on all my delete buttons even if i develop a confirmation page later on. It prevents even me from clicking the delete button by mistake :) .

I hope this has been of some help to you.

Cheers

Related Posts

blog comments powered by Disqus