javascript 如何找出用户使用的浏览器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4825498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:59:46  来源:igfitidea点击:

How can I find out which browser a user is using?

javascriptbrowser-detection

提问by

I would like to show a pop-up telling the user if their browser is outdated. How can I find out what browser they are using in javascript?

我想显示一个弹出窗口,告诉用户他们的浏览器是否已过时。如何找出他们在 javascript 中使用的浏览器?

采纳答案by Venkatesh Appala

function BrowserDetection() {

        if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {

            var ffversion = new Number(RegExp.) ;     
        }

        else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {

            var ieversion = new Number(RegExp.);       
        }

        else if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
            var chromeversion = new Number(RegExp.);
            // capture x.x portion and store as a number

        }
        else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {

            var oprversion = new Number(RegExp.) 
        }
        else if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
            var safariversion = new Number(RegExp.);


        }

        }

Then after finding the version, u will compare and display popup according to your need.

然后找到版本后,您将根据需要进行比较并显示弹出窗口。

回答by jmort253

Check out this browser detection script:

查看此浏览器检测脚本:

http://www.quirksmode.org/js/detect.html

http://www.quirksmode.org/js/detect.html

回答by a paid nerd

Don't detect browsers, detect browser features. There's a good discussion on Stack Overflow already:

不要检测浏览器,检测浏览器功能。Stack Overflow 上已经有一个很好的讨论:

Browser detection versus feature detection

浏览器检测与特征检测