본문 바로가기

나의 플랫폼/Spring Framework

[JavaScript] Detecting Mobile browser

JavaScript 에서 모바일 브라우저를 찾는데 참고할만한 좋은 소스인듯 하여 공유 합니다.


/**
 * Determine the mobile operating system.
 * This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'.
 *
 * @returns {String}
 */
function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

      // Windows Phone must come first because its UA also contains "Android"
    if (/windows phone/i.test(userAgent)) {
        return "Windows Phone";
    }

    if (/android/i.test(userAgent)) {
        return "Android";
    }

    // iOS detection from: http://stackoverflow.com/a/9039885/177710
    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "iOS";
    }

    return "unknown";
}

StackOverFlow : https://stackoverflow.com/a/21742107


참고하세요.