Question

I'm developing a mobile version of a website. I'm currently using this Javascript to detect and redirect the user:

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

Works fine with iPhone and iPod, but no success with Android. I'm using the Android Emulator within Eclipse. I don't have an Android gadget to actually test it.

Am I doing something wrong? Anyone having the same issue?

Was it helpful?

Solution

You should use location.replace instead of window.location

Example:

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

I used this code and it works on iphone/itouch and android phones/devices.

OTHER TIPS

Here is mine JavaScript function to detect Android device:

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 doesn't work in Android? Really? Appears to work just fine for me on Android 2.3.4. What version(s) of Android did you guys use where you couldn't get window.location to succesfully load a new URL in the browser?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top