crimsoniris
crimsoniris

Reputation: 84

Load different JS using JSP according to Internet Explorer version

I'm kinda new to JSP and right now I'm stumped on a problem that most of us wouldn't wish to encounter: Internet Explorer 6.0 support.

I've scoured over Google and found no answer. My question is, is there a way to load a different Javascript inside a JSP according to the browser version?

My algorithm would be: Check Internet Explorer version. IF IE6, load ie6.js ELSE load normal.js.

So far, this is how I load my JS.

<script type="text/javascript" src="../js/normal.js"></script>

Upvotes: 1

Views: 4018

Answers (3)

NSPKUWCExi2pr8wVoGNk
NSPKUWCExi2pr8wVoGNk

Reputation: 2559

If you want to do this on the server side, you would have to check User-Agent HTTP header. After this you could set some attribute to current request and later check that attribute in JSP page:

...
String browserString = httpServletRequest.getHeader("User-Agent");

String browserVersion = ...; // Some user agent parsing and version determining

httpServletRequest.setAttribute("browserVersion", browserVersion);

User agent examples can be found on internet, e.g. http://www.useragentstring.com/pages/Internet%20Explorer/.

Upvotes: 0

Uooo
Uooo

Reputation: 6334

You can use conditinal comments to load different Javascript for Internet Explorer.

So, for your case:

<!--[if !IE 6]><!-->
    <script type="text/javascript" src="normal.js" />
<!--<![endif]-->
<!--[if IE 6]>
    <script type="text/javascript" src="ie6.js" />
<![endif]-->

Upvotes: 1

sohel khalifa
sohel khalifa

Reputation: 5578

You can do this by navigator object like this:

     var ua = navigator.userAgent;
     if(navigator.appName == 'Microsoft Internet Explorer'){
            //check for version

            var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null){
              version = parseFloat( RegExp.$1 );
            }
            if(version == 6.0){
               //load specific js for IE 6.0
               var file = document.createElement("script");
               file.setAttribute("type", "text/javascript");
               file.setAttribute("src", "../js/ie_6.js");
               document.getElementsByTagName("head")[0].appendChild(file);
            }
            else{
                //load normal.js for other IE versions.
                var file = document.createElement("script");
                file.setAttribute("type", "text/javascript");
                file.setAttribute("src", "../js/normal.js");
                document.getElementsByTagName("head")[0].appendChild(file);
            }
     }

Upvotes: 0

Related Questions