【问题标题】:Redirect Specific URL to another URL on moblile only仅在移动设备上将特定 URL 重定向到另一个 URL
【发布时间】:2014-10-24 18:01:03
【问题描述】:

我建立了一个响应式网站,主页在移动设备上是多余的,我希望能够重定向到特定的内部页面。我找到了重定向到移动版网站的解决方案,但这不起作用,因为我遇到了重定向循环,或者您无法访问该网站上的任何其他页面。

例如我想重定向: http://www.url.com/home

http://www.url.com/portfolio

仅在移动设备上。而且只有那一页。

【问题讨论】:

标签: javascript php html mobile responsive-design


【解决方案1】:

将此代码插入http://www.url.com/home 文件

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) || navigator.userAgent.match(/WPDesktop/i);
},
any: function() {
    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}};
if( isMobile.any() ) {
    window.location.assign("http://www.url.com/portfolio");
}

【讨论】:

    【解决方案2】:

    这可以通过使用一些 JavaScript 轻松实现。 if 语句中的代码检测用户是否使用移动设备(Android、iOS、Kindle、BlackBerry、Windows Phone、Kindle 等),if 语句中的代码只是将用户重定向到您指定的链接。

    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)) {
        window.location.replace("http://www.url.com/portfolio");
    }
    

    【讨论】:

      【解决方案3】:

      如果您使用的是 php,则有一些库可以进行移动检测。 Google 实际上提供了一个:https://code.google.com/p/php-mobile-detect/

      在将任何内容发送到客户端之前,在移动设备上检测将位置设置为 http://www.url.com/portfolio

      如果是 javascript,这有点棘手,但您可以大致了解用户代理并将变量设置为哪种设备。

      What is the best way to detect a mobile device in jQuery?

      一旦有了该变量,您就可以对重定向 URL 执行 document.location。

      【讨论】:

      • 链接的 SO 线程也有几个关于 UA 嗅探的缺点的 cmets。这里有一个更可靠的方法(并且可能更高效,因为它不使用庞大的 RegEx):stackoverflow.com/a/4819886/258598(请务必查看 cmets)。
      猜你喜欢
      • 1970-01-01
      • 2019-04-09
      • 2018-05-29
      • 2015-10-28
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      相关资源
      最近更新 更多