0

I have 3 index pages say

index_ch.jsp ,index_ie.jsp ,index_me.jsp

and a main parent page named

browserdetect.jsp

when the user first enters my url in a browsere browserdetect.jsp will run ...... what i need is the jquery or java script that can be put in my browserdetect.jsp which will first detect the browser the user uses and then redirect to the respective index pages based on the browser he or she uses ...... can any one help me out please

Vicky
  • 694
  • 1
  • 8
  • 23

4 Answers4

5

Adding this script to my head section helped me to do what i wanted .... thank you for the help guys ........

    if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0))
{
    window.location.replace("your page");
}
else if (navigator.userAgent.indexOf('Chrome') >= 0) 
{
    window.location.replace("your page");
}
else 
{
   window.location.replace("your page");
}
        </script>
Vicky
  • 694
  • 1
  • 8
  • 23
1

This code helps you to detect browser of user.

var x = "User-agent header sent: " + navigator.userAgent;

1

I guess you want to detect brower of user

if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0))
{
    the code of index_ie.jsp...
}
else if (navigator.userAgent.indexOf('Chrome') >= 0) 
{
    the code of index_ch.jsp...
}
else 
{
    the code of index_me.jsp...
}
Jacky
  • 26
  • 2
  • should i be using window.location.replace("http://mysite/index_ie.jsp"); in the place wher u have written "the code of index_ie.jsp..." in order for me to redirect to my page? and also does the >=0 refer to the browser version? – Vicky Nov 22 '13 at 05:19
  • Yes,but the code should be window.location.href.replace("mysite/browserdetect.jsp","mysite/index_ie.jsp"); and the (navigator.userAgent) include the browser version. – Jacky Nov 22 '13 at 10:03
  • I want to know do you hope include index_ie.jsp or redirect to it in browserdetect.jsp. – Jacky Nov 22 '13 at 10:08
  • just redirect it .... its a seperate jsp with many content in it .... each of the index has different CSS based on the browser – Vicky Nov 22 '13 at 10:29
  • So,you can use in jsp or window.location.href=window.location.href.replace("browserdetect.jsp","index_ie.jsp") in javascript. – Jacky Nov 23 '13 at 00:53
0
browserdetect.jsp ------page

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

    <script type="text/javascript">
    function detectBrowser(){
        var nAgent = navigator.userAgent;
        var verOffset;

            if ((nAgent.indexOf("MSIE"))!=-1) {
             browserName = "Microsoft Internet Explorer";
              window.location = "index_ie.jsp";
            }
            else if ((verOffset=nAgent.indexOf("Chrome"))!=-1) {
             browserName = "Chrome";
                window.location = "index_ch.jsp";
            }
            else if ((verOffset=nAgent.indexOf("Firefox"))!=-1) {
             browserName = "Firefox";
                 window.location = "index_me.jsp";
            }
    }
</script>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body onload="detectBrowser()">
    <h1>Hello World!</h1>
 </body>
</html>

===============================================================================
index_ch.jsp  -----page
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Hello World! Chrome</h1>
</body>
</html>

=========================================================================
index_ie.jsp  -----page


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Hello World! Internet Explorer</h1>
</body>
</html>

=============================================================================
index_me.jsp  -----page

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Hello World! Mozilla Firefox</h1>
</body>
</html>



you can use  jsp redirecting tags 
1. jsp:forward   :- server side redirect [not show index_ie.jsp in the URL]
2. response.sendRedirect :-browser side redirect[ show index_ie.jsp in the URL]

instead of window .location.
Abdul Rahman
  • 952
  • 8
  • 5