Simplified JavaScript Method to Detect Mobile Access

javascript mobile detectionuser agent parsingredirect to mobile siteiPhone Android detectionresponsive web design
Published·Modified·
var ua = navigator.userAgent;
var ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
    isIphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
    isAndroid = ua.match(/(Android)\s+([\d.]+)/),
    isMobile = isIphone || isAndroid;

if (isMobile) {
    location.href = 'http://m.domain.com';
} else {
    location.href = 'http://www.domain.com';
}

// Alternatively, check for iPhone or Android separately
if (isIphone) {
    // code
} else if (isAndroid) {
    // code
} else {
    // code
}

Source: Simplified JavaScript Method to Detect Mobile Access - PointNet. All rights reserved by the original author. If there is any infringement, please contact QQ: 337003006 for deletion.