javascript - iOS/Android 检测和重定向

标签 javascript redirect operating-system

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

这些片段够吗?

<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

最佳答案

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";
}

关于javascript - iOS/Android 检测和重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17522147/

相关文章:

javascript - swiper - 如何禁用嵌套的水平滑动器进一步滚动(边缘触摸释放)?

javascript - 使用这种语法的原因是什么 (0, _.Em)();

multithreading - 多线程模型之间的差异

c - 系统调用不同的硬件架构?

javascript - 使用选择框突出显示匹配的 html 单元格

javascript - 脚本完成后修改动态元素

apache - .htaccess重写规则导致无限循环

redirect - 仅 SSL 的 nginx 重写规则

ruby-on-rails - rails 4 : How to set cookies in redirect?

reference - 按引用而不是按值复制文件夹?