How to find the operating system version using JavaScript?

后端 未结 13 903
滥情空心
滥情空心 2020-11-22 04:32

How can I find the OS name and OS version using JavaScript?

相关标签:
13条回答
  • 2020-11-22 05:29

    Use detectOS.js:

    var Detect = {
        init: function () {
            this.OS = this.searchString(this.dataOS);
        },
        searchString: function (data) {
            for (var i=0;i<data.length;i++) {
                var dataString = data[i].string;
                var dataProp = data[i].prop;
                if (dataString) {
                    if (dataString.indexOf(data[i].subString) != -1)
                        return data[i].identity;
                }
                else if (dataProp)
                    return data[i].identity;
            }
        },
        dataOS : [
            {
                string: navigator.platform,
                subString: 'Win',
                identity: 'Windows'
            },
            {
                string: navigator.platform,
                subString: 'Mac',
                identity: 'macOS'
            },
            {
                string: navigator.userAgent,
                subString: 'iPhone',
                identity: 'iOS'
            },
            {
                string: navigator.userAgent,
                subString: 'iPad',
                identity: 'iOS'
            },
            {
                string: navigator.userAgent,
                subString: 'iPod',
                identity: 'iOS'
            },
            {
                string: navigator.userAgent,
                subString: 'Android',
                identity: 'Android'
            },
            {
                string: navigator.platform,
                subString: 'Linux',
                identity: 'Linux'
            }
        ]
    };
    
    Detect.init();
    
    console.log("We know your OS – it's " + Detect.OS + ". We know everything about you.");
    
    0 讨论(0)
提交回复
热议问题