2012年4月9日 星期一

判斷不同瀏覽器並顯示出來

利用以下的語法就可以判斷是何者瀏覽器 並在span中顯示出來
其後更可以利用此flag來改變css中的值來做不同瀏覽器有不同的css style

<span id="show" ></span>
<script type="text/javascript">
    var isIE = navigator.userAgent.search("MSIE") > -1;  
    var isIE6 = navigator.userAgent.search("MSIE 6") > -1;
    var isFirefox = navigator.userAgent.search("Firefox") > -1;
    var isOpera = navigator.userAgent.search("Opera") > -1;
    var isSafari = navigator.userAgent.search("Safari") > -1;//Google瀏覽器是用這核心
    var browserType=null;
    var chrome = "by_Chrome";
    var IE9 = "by_IE9";
    var IE6 = "by_IE6";
    if (isIE) {
        if (!!window["performance"]){
        browserType = IE9;
        //alert(browserType);
        document.getElementById("show").innerHTML = browserType;
    }   else if(isIE6){
         browserType = IE6;
         document.getElementById("show").innerHTML = browserType;
        }else
         document.getElementById("show").innerHTML = "isIEOthers";
        //alert('isIE_Others');
    }
    if (isFirefox) {
        document.getElementById("show").innerHTML = "by_Firefox";
    }
    if (isOpera) {
        document.getElementById("show").innerHTML = "by_Opera";
    }
    if (isSafari) {
        browserType = chrome;
        document.getElementById("show").innerHTML = browserType;
    }        
</script>

沒有留言:

張貼留言