0

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";
}
John G
  • 55
  • 1
  • 7

1 Answers1

0

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.

Alex
  • 11,115
  • 12
  • 51
  • 64
  • thank you for the spot-on response :-) wurfl.io looks like the way to go with all the new device types being added such as smart watches and smart tVs etc. One question though - if the client browser has Javascript turned off - specially on Microsoft devices, then this would not work, right? Is there a second line of defense you can recommend? – John G Dec 17 '14 at 14:22
  • I developed for Windows Phone 8+ too. But I don't know about disabled javascript in Mobile IE. There is no such option in it. Also note `wurfl.io` use javascript, and create an object in window scope - `WURFL`, and if javascript is disabled you code will not run and will not use `WURLF`. – Alex Dec 17 '14 at 14:34
  • Ok great - I am new to coding for Windows mobile so I wanted to clarify. Thank you very much for the excellent answer and solving my challenge. Happy Holidays ! – John G Dec 17 '14 at 14:39