Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add script to the body (similar functionality like method renderHead(Component component, IHeaderResponse response))

Tags:

wicket

With the following code, I add trackingpixel to some page page.html by overriding the method renderHead(Component component, IHeaderResponse response). This works fine.

page.html looks like this:

    <!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 adds a trackingpixel to the main page. Right mouse click on the page -> source code shows that the following script is added to the page:

<script type="text/javascript" >

Wicket.Event.add(window, "domready", function(event) {      
            WebtrekkInstance = {
             ..   
                         'path' : 'anyPath',
                 ...:...
                              ..
            };
                        ..            
            ;});
</script>

Now I would like to add trackingpixel to a popup. My problem is that I can't add a script to the body. The method renderHead(Component component, IHeaderResponse response) doesn't do that, because (I guess) the popup pops up on the same page, so there is only one head and it will not render twice. So I tried to do this with WebMarkupContainer as you can see below.

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 looks like this:

  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
 }

The problem here is that I cannot add the scriptContainer. add(scriptContainer); will not work, because OurServicePopupPixel is a behaviour and not a page.

like image 731
Kristopher Peter Avatar asked Dec 08 '12 09:12

Kristopher Peter


1 Answers

Maybe you can simple use a Label component with setEscapeModelStrings(false). But it seems a bit strange.

I didn't full understand what is your javascript doing, but maybe you can try to execute it when the DOM is ready. Using a renderHead like this:

public void renderHead(IHeaderResponse response) {
    response.render(OnDomReadyHeaderItem.forScript( ... YOUR SCRIPT HERE ... ));
}

I hope it helps.

like image 105
jordeu Avatar answered Oct 11 '22 14:10

jordeu