[thelist] Triggering an alert box

Phil Turmel philip at turmel.org
Sun Mar 27 08:00:12 CST 2005


Chris Kavanagh wrote:
> To business: I have an <A HREF> on my intranet site that deletes an 
> entry in the backend database.  The deletion can't be undone, so it 
> might be nice to have an alert box that warns of such before actually 
> completing the action.  What's the best way to do this?  I'm using 
> ColdFusion, but maybe the best solution is JavaScript?

I use a combination of a normal HREF and javascript w/ a confirm box. 
The confirmation adds another GET argument to the link if the user 
agrees, and returns true.  If the user cancels, the link is left 
unmodified, and returns false.

If javascript is off, it gracefully degrades:  The backend gets the link 
immediately, notes the missing "Confirmed=1", and throws an intermediate 
form to get confirmation.  Since the javascript is only adding a "GET" 
component, the same routine can be used with numerous such links in a list.

Samples below (names changes, otherwise straight from one of my projects)

HTH,

Phil



PHP to create the links looks like this:

<a class="ConfDel" href="Item.php?Delete=<?php echo $ItemID ?>">Delete</a>

Reasonably cross-browser javascript to go with it: (in or referenced by 
script tag in the page footer)

// Confirm user's desire to request a delete, submit the link
// if user is sure.
function ConfirmDelete() {
if (arguments.length>0) {
     if (arguments[0] instanceof Event)
         var anchor=this;
     else
         var anchor=arguments[0];
     }
else
     var anchor=this;
if (confirm('This item will be permanently deleted.  Proceed?')) {
     anchor.href += '&Confirmed=1';
     return true;
     }
return false;
}

// Set the onclick attribute on all the anchor tags that need it:
var anch;
var anchs = document.getElementsByTagName('a');
for (var i=0; i<anchs.length; i++) {
	anch = anchs[i];
	if (anch.className=='ConfDel')
		anch.onclick=ConfirmDelete;
	}



More information about the thelist mailing list