代码改变世界

js浏览器判断选择(转载)

2012-11-10 23:50  lyw90  阅读(208)  评论(0编辑  收藏  举报

function browser() {
    var A = function() {
        var B = navigator.userAgent.toLowerCase(),
        D = function(F, G) {
            if (!F.className) {
                F.className = G
            } else {
                var E = F.className;
                E += (" " + G);
                F.className = E
            }
        },
        C = function(E) {
            return (B.indexOf(E) != -1)
        };
        D(document.getElementsByTagName("html")[0], [(!(/opera|webtv/i.test(B)) && /msie (\d)/.test(B)) ? ("ie ie" + RegExp.$1) : C("firefox/2") ? "gecko ff2": C("firefox/3") ? "gecko ff3": C("gecko/") ? "gecko": C("chrome/") ? "chrome": C("opera/9") ? "opera opera9": /opera (\d)/.test(B) ? "opera opera" + RegExp.$1: C("konqueror") ? "konqueror": C("applewebkit/") ? "webkit safari": C("mozilla/") ? "gecko": "", (C("x11") || C("linux")) ? " linux": C("mac") ? " mac": C("win") ? " win": ""].join(" "))
    } ()
}


if (window.addEventListener) {
    window.addEventListener("load", browser, false)
} else {
    if (window.attachEvent) {
        window.attachEvent("onload", browser)
    }
};

 

在js判断浏览器后,比如判断为chrome,则<html xmlns="http://www.w3.org/1999/xhtml" class="chrome">,利用class chrome 定义差别。