2

I am writing a web app using HTML and Wicket.

In my HTML page, I have a small alert script and I need that the wicket will insert a value to the message of the alert. My code is:

HTML code:

<script>
   function version(){
   alert(<sub wicket:id="version">);
   } 
</script>
<img class="logo" src="images/logo.png" alt="" width="68" height="21" onclick="version()"/>

Java:

add(new Label("version","1.0.0");

But this is not working.

1 Answer 1

6

Wicket won't process anything within <script>. But you can add a JavaScript function via a header contribution in your page class (or any other Component).

Wicket 1.5:

@Override
public void renderHead(IHeaderResponse response) {
    super.renderHead(response);
    response.renderJavaScript("function version(){alert('" + "1.0.0" + "');}", "version");
}

Wicket 6.0:

@Override
public void renderHead(IHeaderResponse response) {
    super.renderHead(response);
    response.render(JavaScriptHeaderItem.forScript("function version(){alert('" + "1.0.0" + "');}", "version"));
}

An alternative is to render a Label which outputs the function:

add(new Label("script", "function version(){alert('1.0.0');}").setEscapeModelStrings(false));

And in your markup

<script wicket:id="script" />
<img class="logo" src="images/logo.png" alt="" width="68" height="21" onclick="version()"/>
Sign up to request clarification or add additional context in comments.

1 Comment

setEscapeModelStrings sets if component must replace "sensible" characters from its model with their related HTML code. This is done for security reason, i.e. to prevent user from injecting malicious code into our pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.