繁体   English   中英

在将浏览器用户代理与JSON中每个版本的用户代理列表匹配之后,检测页面加载时的操作系统和版本

[英]Detection of OS and version on page load afer matching the browser User Agent with the list of user agents for each version in JSON

我有一个JSON数组-Level1 >> Level2 >> Level3。 Level1将具有所有操作系统名称,例如Windows,Linux等。对于每个操作系统,我们都有2级阵列,每个类别下都有其操作系统和版本。 Windows 10 64位在第2级内部,我们具有用户代理列表。我们必须将浏览器用户代理与JSON中的userAgent列表进行匹配,并显示为检测到的操作系统。 我的代码-

    matchTheOSVersionWithUserAgent: function (osVersion){
    var userAgentList = osVersion.userAgent;
    var matchRes;       
    for(i in userAgentList){        

        matchRes = navigator.userAgent.match(userAgentList[i]);             

  }

模拟JSON:

osPlatforms: [{

        "name": "Windows",
        "osVersions": [{            
            "name": "Windows 10 (32 bit)",
            "osBit": "32",
            "userAgent": ["Windows 10 (32 bit)", "win10", "Microsoft Windows 10", "Windows NT 10.0",  "Microsoft Windows 10 (64-bit)"],


        }, {                
            "name": "Windows 10 (64 bit)",
             "osBit": "64",
            "userAgent": ["win10", "Microsoft Windows 10", "Windows NT 10.0", "Microsoft Windows 10 (32-bit)"],


        }]

我正在获取matchRes,因为null并且没有发生检测。我尝试在Mac和Windows中检查。有人可以对此进行帮助吗.TIA。

您需要这样做的是:

 var osPlatforms = [{ "name": "Windows", "osVersions": [{ "name": "Windows 10 (32 bit)", "osBit": "32", "userAgent": ["Windows 10 (32 bit)", "win10", "Microsoft Windows 10", "Windows NT 10.0", "Microsoft Windows 10 (64-bit)", "Windows NT 6.1"], }, { "name": "Windows 10 (64 bit)", "osBit": "64", "userAgent": ["win10", "Microsoft Windows 10", "Windows NT 10.0", "Microsoft Windows 10 (32-bit)"], }] }]; function matchTheOSVersionWithUserAgent(osVersion) { console.log(navigator.userAgent); var userAgentList = osVersion.userAgent; var matchRes; for (var i = 0; i < userAgentList.length; i++) { if (navigator.userAgent.match(userAgentList[i])) { matchRes = navigator.userAgent.match(userAgentList[i]); break; } } console.log(matchRes); } matchTheOSVersionWithUserAgent(osPlatforms[0].osVersions[0]); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

找到结果后,基本上break循环。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM