【问题标题】:iOS/Android Detect and RedirectiOS/Android 检测和重定向
【发布时间】:2013-07-08 08:34:10
【问题描述】:

js 新手,所以慢慢来:D 需要根据用户使用的操作系统进行重定向。如果 ios 重定向到 x,如果 android 重定向到 y,否则..留在原始地址。 我的问题:

这些sn-ps够吗?

<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>

谢谢!:D

【问题讨论】:

    标签: javascript redirect operating-system


    【解决方案1】:
    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";
    }
    

    【讨论】:

    • 对php版本感到好奇:D设法找到了一些东西但不起作用:(
    • 不能在 android 5.0.1/chrome 46.0 中工作,除非 document.location.href = "y"; 绑定在点击事件中
    猜你喜欢
    • 2019-01-25
    • 2014-09-21
    • 2014-03-11
    • 2017-11-02
    • 2016-02-11
    • 2012-05-10
    • 2015-03-04
    • 2011-09-25
    • 2022-12-20
    相关资源
    最近更新 更多