views:

3819

answers:

7

I'm making a simple remove link with an onClick event that brings up a confirm dialog. I want to confirm that the user wants to delete an entry. However, it seems that when Cancel is clicked in the dialog, the default action (i.e. the href link) is still taking place, so the entry still gets deleted. Not sure what I'm doing wrong here... Any input would be much appreciated.

EDIT: Actually, the way the code is now, the page doesn't even make the function call... so, no dialog comes up at all. I did have the onClick code as:

onClick="confirm('Delete entry?')"

which did bring up a dialog, but was still going to the link on Cancel.

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%&gt;
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt"%&gt;
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<script type="text/javascript">

function delete() {
    return confirm('Delete entry?')
}

</script>


...

<tr>
 <c:if test="${userIDRO}">
    <td>
        <a href="showSkill.htm?row=<c:out value="${skill.employeeSkillId}"/>" />
     <img src="images/edit.GIF" ALT="Edit this skill." border="1"/></a>
    </td>
    <td>
        <a href="showSkill.htm?row=<c:out value="${skill.employeeSkillId}&remove=1"/>" onClick="return delete()"/>
        <img src="images/remove.GIF" ALT="Remove this skill." border="1"/></a>
    </td>
 </c:if>
</tr>

A: 

I use this, works like a charm. No need to have any functions, just inline with your link(s)

onclick="javascript:return confirm('Are you sure you want to delete this comment?')"
smazurov
There is also no need to use "javascript:". But like siukurnin said, you should use POST if you change something.
some
+2  A: 

First of all, delete is a reserved word in javascript, I'm surprised this even executes for you (When I test it in Firefox, I get a syntax error)

Secondly, your HTML looks weird - is there a reason you're closing the opening anchor tags with /> instead of just > ?

Peter Bailey
Wow, brain fart. Didn't think to check 'delete' as a keyword. Kind of a "duh" when so many other languages have it reserved. :P
kchau
XHTML uses /> to close empty tags... That means that the in the code from the original post, the a-tags are closed twice!!
some
+2  A: 

Using a simple link for an action such as removing a record looks dangerous to me : what if a crawler is trying to index your pages ? It will ignore any javascript and follow every link, probably not a good thing.

You'd better use a form with method="POST".

And then you will have an event "OnSubmit" to do exactly what you want...

siukurnin
This is an excellent point. Not only is it potentially dangerous to your application, it's actually a violation of the HTTP spec.
Peter Bailey
JW. Can a crawler access records without being able to login?
kchau
No, crawlers would not be able to access protected portions of your site.
Benry
It the "protection" depends on javascript, the crawler (and anyone who has turned of javascript) can access it.
some
A: 

I've had issue with IE7 and returning false before.

Check my answer here to another problem: Javascript not running on IE

GoodEnough
+4  A: 

There's a typo in your code (the tag a is closed too early). You can either use:

<a href="whatever" onclick="return confirm('are you sure?')"><img ...></a>

note the return: the value returned by scripts in intrinsic evens decides whether the default browser action is run or not; in case you need to run a big piece of code you can of course call another function:

<script type="text/javascript">
function confirm_delete() {
  return confirm('are you sure?');
}
</script>
...
<a href="whatever" onclick="return confirm_delete()"><img ...></a>

(note that delete is a keyword)

For completeness: modern browsers also support DOM events, allowing you to register more than one handler for the same event on each object, access the details of the event, stop the propagation and much more; see DOM Events.

Luca Tettamanti
Thanks, didn't notice the typo.
kchau
A: 

Thanks, paranic! Works like a charm. Tested on Firefox.

+2  A: 

Hi all,

Well, i used to have the same problem and the problem got solved by adding the word "return" before confirm:

onClick="return confirm('Delete entry?')"

I wish this could be heplful for you..

Good Luck!