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) {
super.renderHead(response);
response.renderOnLoadJavascript("alert('it works!')");
}
});