如何根据用户使用的浏览器重定向我的页面?

时间:2013-11-22 04:46:35

标签: javascript jquery jsp

我有3个索引页面说

  

index_ch.jsp   ,index_ie.jsp   ,index_me.jsp

以及名为

的主要父页面
  

browserdetect.jsp

当用户第一次在browsere中输入我的url时,browserdetect.jsp会运行......我需要的是可以放入我的browserdetect.jsp的jquery或java脚本,它将首先检测用户的浏览器使用然后根据他或她使用的浏览器重定向到相应的索引页面......任何人都可以帮助我

4 个答案:

答案 0 :(得分:5)

将这个脚本添加到我的头部帮助我做我想做的事....感谢帮助人........
 

    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>

答案 1 :(得分:1)

此代码可帮助您检测用户的浏览器。

var x =“发送的User-agent标头:”+ navigator.userAgent;

答案 2 :(得分:1)

我想你想要检测用户的浏览器

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...
}

答案 3 :(得分: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.