File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes h:commandbutton javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "h:commandbutton javascript" Watch "h:commandbutton javascript" New topic

h:commandbutton javascript

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
I have a JSF button that when i click this invokes a javascript

Is there a way to ensure that the Action bean is invoked before the javascript
Leigh Taylor

Joined: Feb 19, 2008
Posts: 20
I was always under the impression the 'onclick' gets performed after the action. This isn't the case for you?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17423

Yup. The javascript is performed in the browser by the browser. And since the browser submit function is actually requesting a new page, the javascript is likely to be gone by the time the new page loads. Even if it's just the same page reloading, all the javascripts from the previous copy of the page have been invoked before the submit is done. That's HTML, and in JSF, HTML rules still apply.

On the other hand, if you use an AJAX-style control like the RichFaces command button, the click mechanism can do a partial page request - which is in reality a complete "page" request, but with JavaScript taking the returned data and updating the current page DOM instead of displaying a fresh page. Because the displayed page isn't being replaced - only data is being fetched - it's possible to invoke a javascript function AFTER the AJAX request is complete.

Correction: The "onclick" gets done before the action. One of the things that onclick's can do is abort the action entirely.
[ May 01, 2008: Message edited by: Tim Holloway ]

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: h:commandbutton javascript
It's not a secret anymore!