Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS/Android Detect and Redirect

Newbie in js so take me slow:D Need to make a redirection based on what os the user is using. If ios redirect to x, if android redirect to y, else..stay on the original address. My question:

Are these snippets enough?

<script type="text/javascript"> // <![CDATA[
    if ( (navigator.userAgent.indexOf('Android') != -1) ) {
        document.location = "y";
    } // ]]>
</script>

<script type="text/javascript"> // <![CDATA[
    if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
        document.location = "x";
    } // ]]>
</script>

Thank you!:D

like image 380
Andrei Terecoasa Avatar asked Jul 08 '13 08:07

Andrei Terecoasa


1 Answers

var isMobile = {
        Android: function() {
            return navigator.userAgent.match(/Android/i);
        },
        BlackBerry: function() {
            return navigator.userAgent.match(/BlackBerry/i);
        },
        iOS: function() {
            return navigator.userAgent.match(/iPhone|iPad|iPod/i);
        },
        Opera: function() {
            return navigator.userAgent.match(/Opera Mini/i);
        },
        Windows: function() {
            return navigator.userAgent.match(/IEMobile/i);
        },
        any: function() {
            return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
        }

    };



if ( isMobile.Android() ) {
        document.location.href = "y";
    }
else if(isMobile.iOS())
{
document.location.href="x";
}
like image 177
Kumar Naidu Avatar answered Oct 06 '22 01:10

Kumar Naidu