How to detect my browser version and operating system using JavaScript?

后端 未结 10 957
天命终不由人
天命终不由人 2020-11-22 06:25

I have tried using the code below but it only display results in Chrome and Mozilla not working in IE6.

10条回答
  •  误落风尘
    2020-11-22 07:06

    For Firefox, Chrome, Opera, Internet Explorer and Safari

    var ua="Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)";
    //ua = navigator.userAgent;
    var b;
    var browser;
    if(ua.indexOf("Opera")!=-1) {
    
        b=browser="Opera";
    }
    if(ua.indexOf("Firefox")!=-1 && ua.indexOf("Opera")==-1) {
        b=browser="Firefox";
        // Opera may also contains Firefox
    }
    if(ua.indexOf("Chrome")!=-1) {
        b=browser="Chrome";
    }
    if(ua.indexOf("Safari")!=-1 && ua.indexOf("Chrome")==-1) {
        b=browser="Safari";
        // Chrome always contains Safari
    }
    
    if(ua.indexOf("MSIE")!=-1 && (ua.indexOf("Opera")==-1 && ua.indexOf("Trident")==-1)) {
        b="MSIE";
        browser="Internet Explorer";
        //user agent with MSIE and Opera or MSIE and Trident may exist.
    }
    
    if(ua.indexOf("Trident")!=-1) {
        b="Trident";
        browser="Internet Explorer";
    }
    
    // now for version
    
    
    var version=ua.match(b+"[ /]+[0-9]+(.[0-9]+)*")[0];
    
    console.log("broswer",browser);
    console.log("version",version);
    

提交回复
热议问题