문제

웹 사이트의 모바일 버전을 개발하고 있습니다. 현재이 JavaScript를 사용하여 사용자를 감지하고 리디렉션하고 있습니다.

if((navigator.userAgent.match(/iPhone/i)) || 
                (navigator.userAgent.match(/Android/i)) ||
                (navigator.userAgent.match(/iPod/i))) 
        { 
        window.location = "http://sitename.com/m/";
    }

iPhone 및 iPod에서 잘 작동하지만 Android에서는 성공하지 못합니다. Eclipse 내에서 Android 에뮬레이터를 사용하고 있습니다. 실제로 테스트 할 안드로이드 가제트가 없습니다.

내가 뭔가 잘못하고 있습니까? 같은 문제가있는 사람이 있습니까?

도움이 되었습니까?

해결책

당신은 사용해야합니다 location.replace 대신에 Window.location

예시:

if( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/Android/i)) || (navigator.userAgent.match(/iPod/i)) ) { 
    location.replace("http://sitename.com/m/");
}

이 코드를 사용했으며 iPhone/Itouch 및 Android 전화/장치에서 작동합니다.

다른 팁

Android 장치를 감지하기위한 Mine JavaScript 기능은 다음과 같습니다.

function isAndroid() {
    var ua = navigator.userAgent;
    return ua.match(/Android/) 
        || ua.match(/Dalvik/)
        || ua.match(/GINGERBREAD/)
        || ua.match(/Linux;.*Mobile Safari/)
        || ua.match(/Linux 1\..*AppleWebKit/)
};

Window.location은 Android에서 작동하지 않습니까? 진짜? Android 2.3.4에서 나에게 잘 작동하는 것처럼 보입니다. 브라우저에 새 URL을 성공적으로로드하기 위해 Window.location을 얻을 수없는 곳에서 어떤 버전의 Android 버전을 사용 했습니까?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top