【问题标题】:How can I redirect Desktop Users to one site and Mobile Users to another with JavaScript如何使用 JavaScript 将桌面用户重定向到一个站点,将移动用户重定向到另一个站点
【发布时间】:2015-07-07 06:28:42
【问题描述】:

我希望,如果桌面用户访问我的网站,他们会被重定向到 example.com,而移动用户会被重定向到 m.example.com,我被告知使用此脚本:

<script>

if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i)) { 
return window.location.assign("m.example.com"); 
} else { 
return window.location.assign("example.com"); 
} 
} 
</script>

但是当我访问我的网站时,我得到的只是一个白屏。 这个脚本有问题吗?我需要另一个脚本来处理用户代理还是全部由上述脚本处理? 提前致谢。

【问题讨论】:

标签: javascript redirect mobile desktop


【解决方案1】:

试试这个:

<script>

if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i)) { 
    window.location.assign("http://m.example.com"); 
} else { 
    window.location.assign("http://example.com"); 
} 

</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 2017-02-28
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多