Monday, May 12, 2008

Adding a JavaScript script to the onLoad event using Wicket

After playing around with Wicket for a while and implementing a proof of concept I am now actually involved in my first Wicket project! A first time always comes with its typical problems, but with Wicket it certainly is not hair tearing. You can find a lot of good examples and faq's on the net.
However, I got stuck a while on how to get some javascript to be executed during the onLoad of a page. I could not find an out of the box answer to this. After struggling a bit, it boiled down to this:

add(new AbstractBehavior() {
public void renderHead(IHeaderResponse response) {
response.renderOnLoadJavascript("alert('it works!')");


  1. Thank you, Thank you, Thank You I looked and looked and could not find a way to set focus on a field on page load, from the Wicket Page class.
    Just what I needed :)

  2. wow!!! thaaaank you! really...THANK YOU! ymmd?! ymml!

  3. Perfect.. is it possible to locate a wicket component from the js snippet? by "id" it's not possible because wicket changes dom ids always.. thanks

  4. deprecated...use:

    add(new Behavior() {
    public void renderHead(Component component, IHeaderResponse response) { super.renderHead(component, response); response.renderOnLoadJavaScript("alert('It works!')"); }