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";
}
Use
RegExp
for search inUser Agent
, this cover 99% of modern devices: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.