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

后端 未结 10 941
天命终不由人
天命终不由人 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:16

    I wasn't able to get some of the other answers to work on Chrome, Firefox, IE11, and Edge with the same code. I came up with the below and it appears to work for those browsers listed above. I also wanted to see what OS the user was on. I haven't tested this against a browser with user overridden User-Agent settings, so mileage may vary. The order of the IFs is important for this to work correctly.

    let os, osStore, bStore, appVersion, browser;
    // Chrome
    if(navigator.vendor === "Google Inc."){
        appVersion = navigator.appVersion.split(" ");
        os = [appVersion[1],appVersion[2],appVersion[3],appVersion[4],appVersion[5]].join(" ");
        os = os.split("(")[1].split(")")[0]
        browser = appVersion[appVersion.length-2].split("/").join(" ");
        console.log("Browser:",browser,"- OS:",os);
    }
    
    // Safari
    else if(navigator.vendor === "Apple Computer, Inc."){
        appVersion = navigator.appVersion.split(" ");
        os = [appVersion[1],appVersion[2],appVersion[3],appVersion[4],appVersion[5]].join(" ");
        os = os.split("(")[1].split(")")[0];
        browser = appVersion[appVersion.length-1].split("/").join(" ");
        console.log("Browser:",browser,"- OS:",os);
    }
    
    // Firefox is seems the only browser with oscpu
    else if(navigator.oscpu){
        bStore = navigator.userAgent.split("; ").join("-").split(" ");
        browser = bStore[bStore.length-1].replace("/"," ");
        osStore = [bStore[1],bStore[2],bStore[3]].join(" ");
        osStore = osStore.split("-");
        osStore.pop(osStore.lastIndexOf)
        osStore = osStore.join(" ").split("(");
        os = osStore[1];
        console.log("Browser:",browser,"- OS:",os);
    }
    
    // IE is seems the only browser with cpuClass
    // MSIE 11:10 Mode
    else if(navigator.appName === "Microsoft Internet Explorer"){
        bStore = navigator.appVersion.split("; ");
        browser = bStore[1]+" / "+bStore[4].replace("/"," ");
        os = [bStore[2],bStore[3]].join(" ");
        console.log("Browser:",browser,"- OS:",os);
    }
    
    // MSIE 11
    else if(navigator.cpuClass){
        bStore = navigator.appVersion.split("; ");
        osStore = [bStore[0],bStore[1]].join(" ");
        os = osStore.split("(")[1];
        browser = "MSIE 11 "+bStore[2].split("/").join(" ");
        console.log("Browser:",browser,"- OS:",os);
    }
    
    // Edge
    else if(navigator.appVersion){
        browser = navigator.appVersion.split(" ");
        browser = browser[browser.length -1].split("/").join(" ");
        os = navigator.appVersion.split(")")[0].split("(")[1];
        console.log("Browser:",browser,"- OS:",os);
    }
    
    // Other browser
    else {
        console.log(JSON.stringify(navigator));
    }
    

提交回复
热议问题