JS檢測用戶使用哪種瀏覽器

JS檢測

<html>
<head>
    <title>JavaScript檢測瀏覽器</title>
</head>
<body>
<script type="text/javascript">
    var userAgent=navigator.userAgent.toLowerCase(), s, o = {};   
    var browser={
        version:(userAgent.match(/(?:firefox|opera|safari|chrome|msie)[\/: ]([\d.]+)/))[1],
        safari:/version.+safari/.test(userAgent),
        chrome:/chrome/.test(userAgent),
        firefox:/firefox/.test(userAgent),
        ie:/msie/.test(userAgent),
        opera: /opera/.test(userAgent ) 
    } /* 獲得瀏覽器的名稱及版本信息 */

    if (browser.ie && browser.version > 6)
    {
      /* 判斷是否爲IE 6以上版本,是則執行以下操作 */
      document.writeln("<p>您使用的是IE "+browser.version+"<\/p>");
    }
</script>
</body>
</html>
if (browser.safari) {}  /* 判斷是否爲safari */
if (browser.firefox) {} /* 判斷是否爲firefox */
if (browser.chrome) {}  /* 判斷是否爲chrome */
if (browser.opera) {}   /* 判斷是否爲opera */
if (browser.ie) {}      /* 判斷是否爲IE */

親測可行^_^

PHP檢測

<html>
<head>
    <title>PHP 瀏覽器檢測</title>
</head>
<body>
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "MSIE") ) : ?>

<!-- 這裏就填上你要在IE中執行的html代碼吧 -->

<?php endif; ?>
</body>
</html>
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "MSIE 8.0") ) : ?>     
/* IE 8 */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "MSIE 7.0") ) : ?>    
/* IE 7 */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "MSIE 6.0") ) : ?>     
/* IE 6 */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "NetCaptor") ) : ?>    
/* Netscape */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "Netscape") ) : ?>     
/* Netscape */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "Lynx") ) : ?>         
/* Lynx */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "Opera") ) : ?>       
 /* Opera */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "Konqueror") ) : ?>    
/* Konqueror */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "Mozilla/5.0") ) : ?>  
/* Mozilla/5.0 */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "Firefox") ) : ?>  
/* Firefox */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "Firefox/3") ) : ?>  
/* Firefox 3.0*/
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "Firefox/2") : ?>  
/* Firefox 2.0 */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "Chrome") : ?>  
/* Chrome */
<?php if(strstr($_SERVER["HTTP_USER_AGENT"], "MSIE 6.0") ) : ?>  
<script type="text/javascript">
  alert("還在用Internet Explorer 6 ? 你OUT了,趕快升級吧!")
</script>
<?php endif; ?>

the other way 判斷瀏覽器類別

var Sys={};
var ua=naivigator.userAgent.toLowerCase();
if(window.ActiveXobject)
Sys.ie=ua.match(/msie([\d.]+)/)[1]
else if(doucument.getBoxObjectFor)
Sys.firefox=ua.match(\firefox([\d.]+)/)[1]
else if(window.MessageEvent&&!doucument.getBoxObjectFor)
Sys.chrome=ua.match(\chrome([\d.]+)/)[1]
else if(window.opera)
Sys.opera=ua.match(\opera.([\d.]+)/)[1]
else if(window.openDatabase)
Sys.safari=ua.match(\version([\d.]+)/)[1]
if(document.getBoxObjectFor){
     debugger;
     alert("我是firefox")
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章