Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wicket 6: calling javascript function after page load

This seems so simple, yet I cannot find an example of how to call javascript function from wicket, after the page is loaded (on page that extends WebPage). Can anyone give example on how to go that?

like image 912
user1328889 Avatar asked Jun 20 '13 09:06

user1328889


2 Answers

You can have javascript do that for you

window.onload = function () {
// do stuff here
}

If you need parameters from your wicket page in the javascript function you can override renderHead and add the function there:

@Override
public void renderHead(IHeaderResponse response)
{
    super.renderHead(response);
    String bar = "something";
    response.render(JavaScriptHeaderItem.forScript("window.onload = function () {var foo='" + bar + "'}"));
    // or
    response.render(OnDomReadyHeaderItem.forScript("functionToCall(" + bar + ");") ;
}
like image 161
papkass Avatar answered Oct 25 '22 18:10

papkass


Yet another way to do that is to create AjaxEventBehavior as follows and add it to your page.

AjaxEventBehavior event = new AjaxEventBehavior("onload") {
    @Override
    protected void onEvent(final AjaxRequestTarget target) {
        // do stuff here
        target.appendJavaScript("alert('onload');");
    }
}
add(event);
like image 13
iluwatar Avatar answered Oct 25 '22 17:10

iluwatar