Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

h:commandbutton javascript

 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was always under the impression the 'onclick' gets performed after the action. This isn't the case for you?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic