javascript wicket 6:页面加载后调用javascript函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17210600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wicket 6: calling javascript function after page load
提问by user1328889
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?
这看起来很简单,但在页面加载后(在扩展 WebPage 的页面上),我找不到如何从 wicket 调用 javascript 函数的示例。任何人都可以举例说明如何去做吗?
回答by papkass
You can have javascript do that for you
你可以让javascript为你做这件事
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:
如果您需要 javascript 函数中检票口页面的参数,您可以覆盖 renderHead 并在那里添加函数:
@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 + ");") ;
}
回答by iluwatar
Yet another way to do that is to create AjaxEventBehavior as follows and add it to your page.
另一种方法是按如下方式创建 AjaxEventBehavior 并将其添加到您的页面。
AjaxEventBehavior event = new AjaxEventBehavior("onload") {
@Override
protected void onEvent(final AjaxRequestTarget target) {
// do stuff here
target.appendJavaScript("alert('onload');");
}
}
add(event);