How can you detect the version of a browser?

后端 未结 28 2638
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 23:35

I\'ve been searching around for code that would let me detect if the user visiting the website has Firefox 3 or 4. All I have found is code to detect the type of browser but

28条回答
  •  半阙折子戏
    2020-11-22 00:10

    I wrote this for my needs.

    It get info like if is a mobile device or if has a retina display

    try it

    var nav = {
            isMobile:function(){
                return (navigator.userAgent.match(/iPhone|iPad|iPod|Android|BlackBerry|Opera Mini|IEMobile/i) != null);
            },
            isDesktop:function(){
                return (navigator.userAgent.match(/iPhone|iPad|iPod|Android|BlackBerry|Opera Mini|IEMobile/i) == null);
            },
            isAndroid: function() {
                return navigator.userAgent.match(/Android/i);
            },
            isBlackBerry: function() {
                return navigator.userAgent.match(/BlackBerry/i);
            },
            isIOS: function() {
                return navigator.userAgent.match(/iPhone|iPad|iPod/i);
            },
            isOpera: function() {
                return navigator.userAgent.match(/Opera Mini/i);
            },
            isWindows: function() {
                return navigator.userAgent.match(/IEMobile/i);
            },
            isRetina:function(){
                return window.devicePixelRatio && window.devicePixelRatio > 1;
            },
            isIPad:function(){
                isIPad = (/ipad/gi).test(navigator.platform);
                return isIPad;
            },
            isLandscape:function(){
                if(window.innerHeight < window.innerWidth){
                    return true;
                }
                return false;
            },
            getIOSVersion:function(){
                if(this.isIOS()){
                    var OSVersion = navigator.appVersion.match(/OS (\d+_\d+)/i);
                    OSVersion = OSVersion[1] ? +OSVersion[1].replace('_', '.') : 0;
                    return OSVersion;
                }
                else
                    return false;
            },
            isStandAlone:function(){
                if(_.is(navigator.standalone))
                    return navigator.standalone;
                return false;
            },
            isChrome:function(){
                var isChrome = (/Chrome/gi).test(navigator.appVersion);
                var isSafari = (/Safari/gi).test(navigator.appVersion)
                return isChrome && isSafari;
            },
            isSafari:function(){
                var isSafari = (/Safari/gi).test(navigator.appVersion)
                var isChrome = (/Chrome/gi).test(navigator.appVersion)
                return !isChrome && isSafari;
            }
    }
    

提交回复
热议问题