Click to See Complete Forum and Search --> : Hyperlink Activate Javascript Code Possible?


dan678
10-01-2003, 07:16 PM
<script language=JavaScript><!-- Hide script
function ConfirmDelete() {
if (confirm("Are you sure you want to delete this Caprate?\n" +
"This is your last chance to cancel."))
return true;
return false;
}
//-->
</script>

<form action="#cgi.script_name#" method="POST">
...
<input type="submit" name="delete" value="Delete" tabindex="14" onClick="return ConfirmDelete();">
...

I have a form that lists 1 record from a table. There is a "Delete" button that when the user pushes, a window prompts them asking them if they want to really delete this record.

Can I have a prompt that is similiar to the above javascript activated when a user selects a hyperlink such as:

<a href="del_caprate.cfm?ID=#ID#">Delete</a>

In order to use the above Javascript code, I have to use a button inside a form. Sometimes, I want to be able to use this same prompt when they select a hyperlink that essentially does the same thing. In this case, I don't want to use a form. I need to use a hyperlink using the above code. Basically, I don't want them to accidentally hit this "Delete" hyperlink and deletes the record without a confirmation popping up.

Does anyone know how to tie in the above javascript or something similar to achieve the effect that I'm trying to make? Thanks for any inputs.

P.S. I'm just learning Javascript right now.

Khalid Ali
10-01-2003, 07:27 PM
may be able to do that,just make sure that your server application can recieve get requests

Charles
10-01-2003, 07:28 PM
The trick is in makeing sure that the page will still submit when there is no JavaScript.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<form action="" onsubmit="return confirm('Are you sure you want to delete this Caprate?\nThis is your last chance to cancel.')">
<div>
<script type="text/javascript">
<!--
document.write('<a href="#">Delete</a>');
document.links[document.links.length-1].onclick = function () {if (document.forms[0].onsubmit()) document.forms[0].submit()};
// -->
</script>
<noscript><button type="submit">Delete</button></noscript>
</div>
</form>

snoopy0877
10-01-2003, 11:39 PM
Hi Charles, for me ask one question: can u explain what's function of the script below?

<noscript><button type="submit">Delete</button></noscript>

In my browser( IE6 ), I don't see it done??? Thanks

Charles
10-02-2003, 05:19 AM
The contents of the NOSCRIPT (http://www.w3.org/TR/html4/interact/scripts.html#edef-NOSCRIPT) element will be displayed for the 13% of users who do not use JavaScript. In this case that will give them a submit button to push. You will notice that in my example above the link is drawn with JavaScript. That's so that the link will not appear for those same good 13% of users.