使用以下代码,我page.html
通过覆盖方法将 trackingpixel 添加到某个页面renderHead(Component component, IHeaderResponse response)
。这工作正常。
page.html 看起来像这样:
<!doctype html>
<html xmlns:wicket="http://wicket.apache.org/">
<head>
..
<wicket:container wicket:id="header"></wicket:container>
</head>
<body>
..
<script wicket:id="scriptHolder" type="text/javascript" > I would like to add my script here
</script>
..
</body>
</html>
TrackingPixel.java:
public abstract class TrackingPixel extends AbstractDefaultAjaxBehavior {
protected TrackingPixel(TrackingPixelType type) {
..
}
@Override
public void renderHead(Component component, IHeaderResponse response) {
response.renderOnDomReadyJavaScript("WebtrekkInstance = {
..
'path' : 'anyPath',
...:...
..
};
");
}
}
renderHead-method
向主页添加一个 trackingpixel。在页面上鼠标右键->源码显示页面中添加了如下脚本:
<script type="text/javascript" >
Wicket.Event.add(window, "domready", function(event) {
WebtrekkInstance = {
..
'path' : 'anyPath',
...:...
..
};
..
;});
</script>
现在我想将 trackingpixel 添加到弹出窗口中。我的问题是我无法在正文中添加脚本。该方法renderHead(Component component, IHeaderResponse response)
没有这样做,因为(我猜)弹出窗口在同一页面上弹出,所以只有一个头部并且不会渲染两次。所以我尝试这样做,WebMarkupContainer
如下所示。
OurServicePopup.java
/**
* Class to display our service as popup
*/
public class OurServicePopupPage<T> extends WebPage {
public OurServicePopupPage(PageParameters parameters) {
super(parameters);
}
@Override
protected void onInitialize() {
add(new OurServicePixel());
super.onInitialize();
}
}
OurServicePixel.java 看起来像这样:
public class OurServicePopupPixel extends TrackingPixel{
public OurServicePopupPixel() {
}
WebMarkupContainer scriptContainer = new WebMarkupContainer("scriptContainer");
@Override
public void renderHead(Component component, IHeaderResponse response) {
scriptContainer.add(new AttributeAppender("type", Model.of("text/javascript")));
scriptContainer.add(
new AttributeAppender("src","WebtrekkInstance = {
..
'path' : 'anyPath',
...:...
..
};
");
}
add(scriptContainer); //this shows error
}
这里的问题是我无法添加 scriptContainer。 add(scriptContainer);
将不起作用,因为OurServicePopupPixel
是行为而不是页面。