Rectangle 27 0

How can I execute Javascript before a JSF h:commandLink action is performed?


var a=function(){/*your onclick*/}; var b=function(){/*JSF onclick*/}; return (a()==false) ? false : b();

I am not able to add an onclick attribute to <h:commandLink>. I receive the following error: org.apache.jasper.compiler.CompileException: /MyPage.jsp(164,4) Attribute onclick invalid according to the specified TLD I have also seen a number of other people on forums requesting a solution like this.

So your function wont be passed the event object (which isn't reliable cross browser anyway), but returning true/false will short-circuit the submission.

You can still use onclick. The JSF render kit specification (see Encode Behavior) describes how the link should handle it. Here is the important part (what it renders for onclick):

Note
Rectangle 27 0

How can I execute Javascript before a JSF h:commandLink action is performed?


onclick="return confirm('Are you sure?');"

funny that if constructs are suggested in all posts below by developers with some experience.. and even more funny I'm learning from them :)

Note
Rectangle 27 0

How can I execute Javascript before a JSF h:commandLink action is performed?


onclick="if(confirm('Are you sure?')) return false;" />
onclick="if(confirm(\"Are you sure?\"))return true; else return false;"

@grant-wagner, I realize it is a lot of years now but perhaps implementation you used at the time was broken

I am not able to add an onclick attribute to <h:commandLink>. I receive the following error: org.apache.jasper.compiler.CompileException: /MyPage.jsp(164,4) Attribute onclick invalid according to the specified TLD I have also seen a number of other people on forums requesting a solution like this.

Note
Rectangle 27 0

How can I execute Javascript before a JSF h:commandLink action is performed?


var deleteClick;
var mess="xxx";
function assignDeleteClick(link) {
    if (link.onclick == confirmDelete) {
        return;
    }
    deleteClick = link.onclick;
    link.onclick = confirmDelete;
}


function confirmDelete() {
    var ans = confirm(mess);
    if (ans == true) {
        return deleteClick();
    } else {
        return false;
    }
}
Note
Rectangle 27 0

How can I execute Javascript before a JSF h:commandLink action is performed?


<h:commandLink action="#{bean.action}" onclick="if(confirm('Are you sure?')) return false;" />

Also, other libraries such as MyFaces or IceFaces implement the "onclick" handler.

In JSF 1.2 you can specify onclick events.

Note: you can't just do return confirm(...) as this will block the rest of the JavaScript in the onClick event from happening, which would effectively stop your action from happening no matter what the user returned!

Note
Rectangle 27 0

How can I execute Javascript before a JSF h:commandLink action is performed?


<h:commandLink id="myCommandLink" action="#{myPageCode.doDelete}">
    <h:outputText value="#{msgs.deleteText}" />
</h:commandLink>
<script type="text/javascript">
if (document.getElementById) {
    var commandLink = document.getElementById('<c:out value="${myPageCode.myCommandLinkClientId}" />');
    if (commandLink && commandLink.onclick) {
        var commandLinkOnclick = commandLink.onclick;
        commandLink.onclick = function() {
            var result = confirm('Do you really want to <c:out value="${msgs.deleteText}" />?');
            if (result) {
                return commandLinkOnclick();
            }
            return false;
        }
    }
}
</script>

Other Javascript actions (like validating form input etc) could be performed by replacing the call to confirm() with a call to another function.

Note
Rectangle 27 0

How can I execute Javascript before a JSF h:commandLink action is performed?


<h:commandButton title="#{bundle.NewPatient}" action="#{identifController.prepareCreate}"  
                                             id="newibutton" 
                                             onclick="if(confirm('#{bundle.NewPatient}?'))return true; else return false;" 
                                             value="#{bundle.NewPatient}"/>

Is there an easy way in Primefaces to implement the above using <p:commandLink.../> instead?

That can be simplified as onclick="return confirm('Are you sure?')". It's unnecessary to check a boolean value to return a boolean value. Just return it directly.

Note
Rectangle 27 0

How can I execute Javascript before a JSF h:commandLink action is performed?


var deleteClick;
var mess="xxx";
function assignDeleteClick(link) {
    if (link.onclick == confirmDelete) {
        return;
    }
    deleteClick = link.onclick;
    link.onclick = confirmDelete;
}


function confirmDelete() {
    var ans = confirm(mess);
    if (ans == true) {
        return deleteClick();
    } else {
        return false;
    }
}
Note
Rectangle 27 0

How can I execute Javascript before a JSF h:commandLink action is performed?


var a=function(){/*your onclick*/}; var b=function(){/*JSF onclick*/}; return (a()==false) ? false : b();

I am not able to add an onclick attribute to <h:commandLink>. I receive the following error: org.apache.jasper.compiler.CompileException: /MyPage.jsp(164,4) Attribute onclick invalid according to the specified TLD I have also seen a number of other people on forums requesting a solution like this.

So your function wont be passed the event object (which isn't reliable cross browser anyway), but returning true/false will short-circuit the submission.

You can still use onclick. The JSF render kit specification (see Encode Behavior) describes how the link should handle it. Here is the important part (what it renders for onclick):

Note
Rectangle 27 0

How can I execute Javascript before a JSF h:commandLink action is performed?


onclick="if(confirm('Are you sure?')) return false;" />
onclick="if(confirm(\"Are you sure?\"))return true; else return false;"

@grant-wagner, I realize it is a lot of years now but perhaps implementation you used at the time was broken

I am not able to add an onclick attribute to <h:commandLink>. I receive the following error: org.apache.jasper.compiler.CompileException: /MyPage.jsp(164,4) Attribute onclick invalid according to the specified TLD I have also seen a number of other people on forums requesting a solution like this.

Note