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

How to redirect my page based on the browsers the user uses?

本文关键字:重定向 浏览器 我的 何根 用户      更新时间:2023-09-26

我有3个索引页,比如

index_ch.jsp,index_ie.jsp,index_me.jsp

和一个名为的主父页面

browserdetect.jsp

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

将这个脚本添加到我的头部分帮助我完成了我想要的。。。。谢谢你们的帮助

    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>

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

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