Detect if browser supports javascript or ajax in Wicket?

Wicket build-in a lot of Ajax fallback components that degrade to a normal request if ajax is not available or Javascript is disabled. Some good examples are AjaxFallbackButton, AjaxFallbackLink and etc.

Note
However there are some components that didn’t implement the fallback behavior, for instance AjaxLazyLoadPanel, it will keep display the Wicket’s default image forever if JavaScript is disabled.

Solution

Actually, Wicket has a build-in mechanism to detect the if browser is support JavaScript. But, Wicket turn this function off by default for a reason.

1. Gather your browser’s info

In Wicket application, override init() with setGatherExtendedBrowserInfo(true), it tell Wicket to gather extra information from your browser.


	protected void init() {		
		getRequestCycleSettings().setGatherExtendedBrowserInfo(true);
	}

2. Detect JavaScript

Now, you can detect whether browser is support JavaScript like this :


WebClientInfo clientInfo = (WebClientInfo)WebRequestCycle.get().getClientInfo();
if(clientInfo.getProperties().isJavaEnabled()){
  //Javascript is enable
}else{
 //Javascript is enable
}

Explanation

When user request a page, Wicket will execute a simple JavaScript redirect testing in client browser to test whether browser’s JavaScript is supported, and save it into user’s session. After executed the redirect testing, Wicket will redirect to the original requested page. The whole process is very fast , but you will see a flash of blank page. This is why Wicket turn it off by default. The flash of black page is really annoying, when you seeing this kind of flash page, you may assume this site is a phishing site :), so does your client.

Wicket redirect testing page is generic and suitable in all of the cases, however the flash of a blank page is really unacceptable , at least for me.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
6 Comment threads
4 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
SrihariWicket Tutorialsmkyongsrmdeyb Recent comment authors
newest oldest most voted
Srihari
Guest
Srihari

@mkyong, Thank you on the above info. very useful. we have a problem that we would like your pointers. Problem: The intermediate web page is not getting redirected when the wicket web session idle timeout expires. Although we are able to get redirected to the intermediate page if it a web session idle timeout expiry with remember me option. So basically the web properties are not loaded with the required browser info and most of the info is set to a defaulted value. for e.g. we need the utcOffset to calculate the client’s timezone and in the above case we… Read more »

trackback
Wicket Tutorials

[…] Detect if browser supports JavaScript or Ajax Wicket way to detect if browser supports JavaScript or Ajax […]

deyb
Guest
deyb

is there a way to check if flash is enabled using wicket?

Witi
Guest
Witi

JavaScript is not Java!

This method calls in JavaScript navigator.javaEnabled(). It returns if Java is enabled, e.g. if the Java-Plugin for Java-Applets is installed.

cheers,
Witi

Mehedi Hasan
Guest
Mehedi Hasan

Great post… thanks a lot :D

trackback
How do use AjaxLazyLoadPanel in Wicket? | wicket

[…] How do detect browser javascript or ajax disabled in Wicket? […]