- VolkomenJuist.nl - http://www.volkomenjuist.nl/blog -

How to check whether Javascript is enabled in Wicket

It is possible, via Wicket, to gather a lot of information regarding the browser/system of your visitor. If you want this you can also turn on some extended information (like javascript is enabled) via the following code:

getRequestCycleSettings().setGatherExtendedBrowserInfo(true);

This method works via a redirect to the BrowserInfoPage and finally it goes to your ‘original’ page. Because of this it is possible that you see the BrowserInfoPage very shortly especially on slow computers/browsers or internet connections. But what if you don’t want this and only want to know whether Javascript is enabled? Please read on…

When the page is rendered it does a call(back) to Wicket. If this callback is possible then we can assume Javascipt is enabled.

How to setup?

e.g. Login.java (Page)

final WebClientInfo clientInfo = (WebClientInfo) WebRequestCycle.get().getClientInfo();
final AbstractDefaultAjaxBehavior behavior = new AbstractDefaultAjaxBehavior()
   @Override
   protected void respond(final AjaxRequestTarget target) {
        if (target != null) {
            clientInfo.getProperties().setJavaEnabled(true);
        }
   }
};
add(behavior);

Label jsEnabledScript = new Label("jsEnabledScript", "wicketAjaxGet('"+ behavior.getCallbackUrl() +"', function() { }, function() { });");
jsEnabledScript.setEscapeModelStrings(false); // do not HTML escape JavaScript code
add(jsEnabledScript);

And in your HTML

<script type="text/javascript" wicket:id="jsEnabledScript"></script>

How to use?

final WebClientInfo clientInfo = (WebClientInfo) WebRequestCycle.get().getClientInfo();
boolean javascriptEnabled = clientInfo.getProperties().isJavaEnabled();
Note1: Put this code on one of your first pages like your login page, to do this callback only once.
Note2: I really don’t have a clue why they called it isJavaEnabled() because Java != Javascript….
Buffer [1]
[2]