iOS/Android检测和重定向

And*_*asa 4 javascript redirect operating-system

js中的新手让我慢一点:D需要根据用户使用的os进行重定向.如果ios重定向到x,如果android重定向到y,else..stay在原始地址上.我的问题:

这些片段足够吗?

<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>
Run Code Online (Sandbox Code Playgroud)

谢谢!:D

小智 25

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";
}
Run Code Online (Sandbox Code Playgroud)