Updating device detection code for Windows 8 mobile devices - phones and Surface X

75 views Asked by At

I have used the attached code to detect mobile devices before but it is not working (for obvious reasons) for Windows 8 mobile phones and any of the Surface versions. I don't know the proper userAgent for Windows 8 mobile phones and Surface. What needs to change in order to be able to detect a Surface or a Windows 8 mobile phone? Any help will be greatly appreciated ! Thanks a ton in advance.

function detect() {
var uagent = navigator.userAgent.toLowerCase();
var mobile = false;
var search_strings = [
"iphone",
"ipod",
"ipad",
"series60",
"windows ce",
"windows7phone",
"w7p",
"windows8phone",
"w8p",
"blackberry","
];
for (i in search_strings) {
    if (uagent.search(search_strings[i]) > -1)
        mobile = true;
    }
            return mobile;
}

if (detect()){
    window.location = "mobile";
}
1

There are 1 answers

3
Alex On BEST ANSWER

Use RegExp for search in User Agent, this cover 99% of modern devices:

/Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Symbian|Opera\sM(obi|ini)|Blazer|Dolfin|Dolphin|UCBrowser/.test(navigator.userAgent);

Also you can use service wurfl.io - it will return an object in javascript with flag is device mobile or not. It use wurfl cloud with thousands of known UA.