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

Javascript 如何根据用户使用的浏览器重定向我的页面?,javascript,jquery,jsp,Javascript,Jquery,Jsp,我有3个索引页说 index_ch.jsp ,index_ie.jsp ,index_me.jsp 和一个名为 browserdetect.jsp 当用户第一次在浏览器中输入我的url时,browserdetect.jsp将运行。。。。。。我需要的是jquery或java脚本,可以放在我的browserdetect.jsp中,它将首先检测用户使用的浏览器,然后根据用户使用的浏览器重定向到相应的索引页。。。。。。有人能帮我吗?此代码帮助您检测用户的浏览器 var x=“已发送用户代理标头:”+na

我有3个索引页说

index_ch.jsp ,index_ie.jsp ,index_me.jsp

和一个名为

browserdetect.jsp


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

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


var x=“已发送用户代理标头:”+navigator.userAgent

我猜您想检测用户的浏览器

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...
}
if((navigator.userAgent.indexOf('MSIE')>=0)和&(navigator.userAgent.indexOf('Opera')<0))
{
index_ie.jsp的代码。。。
}
else if(navigator.userAgent.indexOf('Chrome')>=0)
{
index_ch.jsp的代码。。。
}
其他的
{
index_me.jsp的代码。。。
}

将此脚本添加到“我的头”部分帮助我完成了我想要的。。。。感谢大家的帮助……。

if((navigator.userAgent.indexOf('MSIE')>=0)和&(navigator.userAgent.indexOf('Opera')<0))
{
window.location.replace(“您的页面”);
}
else if(navigator.userAgent.indexOf('Chrome')>=0)
{
window.location.replace(“您的页面”);
}
其他的
{
window.location.replace(“您的页面”);
}
browserdetect.jsp------第页
函数detectBrowser(){
var nAgent=navigator.userAgent;
var补偿;
if((nAgent.indexOf(“MSIE”)!=-1){
browserName=“Microsoft Internet Explorer”;
window.location=“index_ie.jsp”;
}
否则如果((verOffset=nAgent.indexOf(“Chrome”)!=-1){
browserName=“Chrome”;
window.location=“index_ch.jsp”;
}
else if((verOffset=nAgent.indexOf(“Firefox”)!=-1){
browserName=“Firefox”;
window.location=“index\u me.jsp”;
}
}
JSP页面
你好,世界!
===============================================================================
index_ch.jsp------第页
JSP页面
你好,世界!铬
=========================================================================
index_ie.jsp------第页
JSP页面
你好,世界!Internet Explorer
=============================================================================
index_me.jsp------第页
JSP页面
你好,世界!Mozilla Firefox
您可以使用jsp重定向标记
1.jsp:forward:-服务器端重定向[不在URL中显示index_ie.jsp]
2.response.sendRedirect:-浏览器端重定向[在URL中显示index_ie.jsp]
而不是窗口。位置。

我应该使用window.location.replace(“);在你写“index_ie.jsp的代码…”的地方,以便我重定向到我的页面?并且>=0是否表示浏览器版本?是的,但代码应该是window.location.href.replace(“mysite/browserdetect.jsp”、“mysite/index_ie.jsp”);以及(navigator.userAgent)包括浏览器版本。我想知道您是否希望在browserdetect.jsp中包含index_ie.jsp或重定向到它。只需重定向它……它是一个单独的jsp,其中包含许多内容……每个索引都有不同的CSS,基于浏览器。因此,您可以在jsp或window.location.href=window.location.href.replace(“browserdetect.jsp”javascript中的索引“\u ie.jsp”)。
    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>
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.