if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) ||
/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent)) {
if(location.href.indexOf('?mobile') < 0) {
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
location.href = '../m/index.html';
} else if(/iPad/i.test(navigator.userAgent)) {
// ipad branch
}
}
}

function getPlatform(){
const ua = navigator.userAgent.toLowerCase();
if(/android/.test(ua)) return 'android';
if(/iphone|ipad/.test(ua)) return 'ios';
if(/windows/.test(ua)) return 'wp';
return 'pc';
}

Modern advice: prefer responsive design + @media queries; UA parsing is brittle.