JavaScript检测浏览器版本

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>检测浏览器版本并提示</title>
 6 <style type="text/css">
 7     
 8 </style>
 9 
10 </head>
11 
12 <body>
13 <div id="tipDiv" style="z-index:10;display:none;vertical-align:middle;text-align:center;background-color:#EEEEEE;width:800px;height:30px;position:absolute;top:5px;left:200px;">
14     <span id="supportHtml5">123123123123133</span>
15 </div>
16 <script type="text/javascript">
17     //判断是否支持HTML5
18     function isGo()
19     {  
20         var isSupport = false;
21         var Sys = {};
22         var ua = navigator.userAgent.toLowerCase();
23         var s;
24         (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
25         (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
26         (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
27         (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
28         (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
29  
30         
31         //以下进行测试
32         //if (Sys.ie) document.write('IE: ' + Sys.ie);
33         //if (Sys.firefox) document.write('Firefox: ' + Sys.firefox);
34         //if (Sys.chrome) document.write('Chrome: ' + Sys.chrome);
35         //if (Sys.opera) document.write('Opera: ' + Sys.opera);
36         //if (Sys.safari) document.write('Safari: ' + Sys.safari);
37         
38         if (Sys.firefox || Sys.chrome || Sys.opera || Sys.safari)
39         {
40             isSupport = true;
41         }
42         else if (Sys.ie)
43         {
44             if(parseInt(Sys.ie) >= 9)
45             {
46                 isSupport = true;
47             }
48             else
49             {
50                 isSupport = false;
51             }
52         }
53         else
54         {
55             isSupport = false;
56         }
57         
58         return isSupport;
59     }
60     
61     //页面跳转
62     function trans()
63     {
64         window.self.location.replace('js2.html');
65     }
66     
67     
68     //具体操作
69     (function(){
70         
71         var result = isGo();
72         var t = document.getElementById("supportHtml5");
73         if(result)
74         {
75             
76             t.innerHTML = '恭喜您,您的浏览器支持HTML5,<input style="border-style:none;cursor:pointer;background-color:#E8E8E8" type="button" value="立即转到HTML5页面" onclick="trans()" /> 5秒后自动关闭';
77         }
78         else
79         {
80             t.innerHTML = "对不起,您的浏览器支持不HTML5,不能访问该页面的HTML5版本,5秒后自动关闭本消息";
81         }
82         document.getElementById("tipDiv").style.display = "block";
83         var closeTip = setTimeout(function(){document.getElementById("tipDiv").style.display = "none";},5000);
84     
85         
86         
87     })();
88    
89 </script>
90 </body>
91 </html>
posted @ 2012-07-13 18:23  常伟华  阅读(607)  评论(0编辑  收藏  举报