Test for Java

When developing a website it’s easy to get in to the trap of testing for your favourite browser.  You may even test your site in  other browsers too but are you testing in the latest version of each browser?  If you are then everything may look to be great.  However there are still a lot of users running IE6 (mainly large corporates using terminal services), older versions of Firefox and Mac users are using outdated version of Safari.

The current trend is moving away from Flash and using Java based client side scripting to produce animated and feature rich websites.  Solutions such as AJAX, JQuery, Mootools are all Java based and require the client browser does not have Java disabled.  If they do then your new fancy website is just going to look broken. It’s always good practice to test for the technology before the user gets to see if it.  If you know they don’t have it at least you then have the option of handling the situation instead of displaying what appears to be a broken site.

Testing for Java is relatively easy.  Assume that Java is not enabled in the first place, test for Java, if this it’s there then action accordingly.

In this example we are going to add two Divs to our html page and give them the IDs of jOFF and jON:

<div id="jOff" >Java is disabled</div>
<div id="jOn" style="visibility:hidden;">Java is enabled</div>

jOff displays our disabled message and jOn displays our enabled message.  As you can see jON has it’s visibility set to hidden.  If the user does not have Java enabled then they will see the jOff message.

What we need to do now is hide the jOFF message and display the jOn message for those users that do have Java enabled.  The code looks like this:

<script  type="text/javascript">
   if (navigator.javaEnabled()) {
      document.getElementById('jOff').style.visibility = 'hidden';
      document.getElementById('jOn').style.visibility = 'visible';
   }
</script>

As  you can see from the code the if condition returns true if Java is enabled where we set the visibility style of jOff to hidden and jOn to visible.

No Comments

Leave a Reply

Your email is never shared.Required fields are marked *