[thelist] js alert to confirm deletion

Joshua Olson joshua at waetech.com
Tue Sep 24 15:04:00 CDT 2002


----- Original Message -----
From: "Pete Freitag" <pf at cfdev.com>
Sent: Tuesday, September 24, 2002 4:00 PM


> this should do the trick...
>
> <script>
> function confirmDelete(delUrl) {
> if (confirm("Are you sure you want to delete")) {
> document.location = delUrl;
> }
> }
> </script>
>
> <a href="javascript:confirmDelete('delete.php...')">Delete</a>

okay, I have to jump in here.  While this code works for js-enabled
browsers, it's probably not in good practice to employ this technique
because it makes it so non-js browsers can never follow the link.

Here is some better code (untested for syntax):

<script language="JavaScript" type="text/javascript">
  function confirmDelete(anchor)
  {
    if (confirm('Are you sure?'))
    {
      anchor.href += '&confirm=1';
      return true;
    }
    return false;
  }
</script>

<a href="deleteit.asp?id=5" onclick="return confirmDelete(this);">Delete<a>

The bigtime benefit to this code is that clicking yes on the javascript
popup actually passes this decision to the target of the anchor in the url.
The processing page can already know that you confirmed the decision because
of the confirm=1 in the url.

If the processing page does not see the confirm=1 in the url, then you now
have the opportunity to do a full blown confirmation page.  This is great
for non-js users.

Just my 2cents.

-joshua




More information about the thelist mailing list