If you've ever tried to add an onclick handler to a JSF h:commandLink component, you'll notice that the event isn't defined. The reason is that JSF uses onclick for it's own purposes, to POST the click where it would normally be a GET request. For example,
<a id="entriesTableView:feedForm:feedTable:0:deleteEntryLink"
href="#"
onclick="clearFormHiddenParams_entriesTableView_feedForm('entriesTableView:feedForm');document.forms[ ... and so on ...
This makes it impossible to do one of the most basic onclick tasks: confirm an action. If you Google for this, the suggestion is to set the onmousedown handler, and use it just as you would onclick:
<h:commandLink action="delete" onmousedown="return confirmDelete(this);">
Nice idea, but it turns out that many browser do not treat the "return false" from the onmousedown event the same way as with onclick. Normally, the false return value means abort the user's click. However, it appears that it does NOT necessarily mean abort the onclick event handler. The result is that the onclick is executed regardless of the return value of the onmousedown event handler.
The solve this, you'll need to do some Javascript trickery. The basic idea is to re-assign the link's onclick handler to a new Javascript function that has a pointer to the original (JSF defined) function. The new function does the ok / cancel logic. An "ok" just calls the original JSF-defined onclick function. "Cancel" returns false from onlick, and aborts the click like we want.
...
var deleteClick;
function assignDeleteClick(link) {
if (link.onclick == confirmDelete) {
return;
}
deleteClick = link.onclick;
deleteMsg = msg;
link.onclick = confirmDelete;
}
function confirmDelete() {
var ans = confirm("Delete?");
if (ans == true) {
return deleteClick();
} else {
return false;
}
}
...
<h:commandLink action="delete" onmousedown="assignDelete(this);">
Note that this re-assigns the onclick event every time the link is clicked. This is not necessary and it could be done once in the onload event. This would require hardcoding of the big long scary JSF component ID, which I do not like doing. This is the reason that the first lines of assignDelete() check to ensure that the assignment hasn't been done already.

Posted by Ryan Lubke on March 19, 2007 at 01:03 PM PDT #
On more comment for JSF 1.2 + commandLink: onclick does not work w/ onclick="someFunction(this)". Because of the encoding strategy, that function is wrapped into another function and 'this' is not the anchor anymore, but some windowObject component.
As a workaround I'm using onmouseup.. it works if all you need is that function to be executed..
Posted by doballve on October 01, 2007 at 10:23 PM PDT #
5
Posted by 203.58.120.11 on June 05, 2008 at 12:12 AM PDT #
This works.
function confirmationDelete(link) {
if (confirmDelete()) {
link.fireEvent('onclick');
return true;
}
return false;
}
function confirmDelete() {
var ans = confirm("Are you sure you want to delete this record?");
if (ans == true) {
return true;
} else {
return false;
}
}
Posted by SG on October 02, 2008 at 04:48 PM PDT #
Hi all,
The only thing that works here, is the SG solution posted on 2nd October 2008. The main example doesn't work, and should not work.
check this line: deleteMsg = msg;
What's that for? It even doesn't compile! This is one other good reason I hope to stay away from JSF soon. No support, too many ways, patches to do simple things.
regards,
Posted by Etienne on October 30, 2008 at 02:31 AM PDT #
jsghdlk;jdlkd;lkd;'dl;'djhkj
Posted by 168.230.2.30 on February 23, 2009 at 04:42 AM PST #
hhhhhhhhhhhhhhhhhhhhhhhssssssssssssssssssssssssssssssssssskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkssssssssssssssssssssssssssssssssmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmhdghfhgdfdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmssssjjjjjjjjjdj,hslkj
Posted by 168.230.2.30 on February 23, 2009 at 04:47 AM PST #