문제

I need to use Javascript to get the Operating System of a user. After some research through the forms I found that I need to use navigator.userAgent to get the information that I require. When I use this a string is returned similiar to: 5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36

What I need specifically is to get the Operating System version (so in the above example Windows NT 6.1) and based on that do some code similar to:

if(operatingSystem = 'Windows NT 5.1'){
//do code
}

So basically I need to parse the user agent string and if the user is on windows XP (Windows NT 5.1) then do some code. I am having difficulty parsing the string though to get just the operating system. I also cant use navigator.oscpu since that will only work for Firefox browsers.

Any help would be greatly appreciated. Thanks.

도움이 되었습니까?

해결책

You can do something like:

if(/Windows NT 5\.1/.test(navigator.userAgent)){
    // do code
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top