移动站点重定向与完整站点链接

4
我正在使用来自http://detectmobilebrowsers.com/的JavaScript版本重定向到移动站点。唯一的问题是,我有一个链接可以进入完整站点,以防万一用户想要或需要去那里。

但是,当您从移动设备点击查看完整站点时,它会重新开始重定向并将其发送回移动版本而不是完整站点。

我进行了一些搜索,并想知道是否可能对其进行黑客攻击,以便它使用

window.location.href.indexOf

或类似这样的某些内容:

或类似这样:

if(window.location.href.indexOf("mobile/index.html") > -1)
{window.location = "http://thefullsiteURL.com"}
else { function (a, b) {
if (//mobile direction stuff from detectmobilebrowsers.com
})(navigator.userAgent || navigator.vendor || window.opera,
'http://thefullsiteURL.com/mobile/index.html')};

请注意,这是我拼凑出来的东西,我的JS技能还比较新,如果有更优雅的解决方案,我完全支持。

1个回答

6

在完整的网站链接中,结合查询字符串值设置会话cookie。然后,让您的移动设备检测代码首先检查cookie值,其次检查查询字符串,最后检查用户代理移动设备检测。

因此,您的完整网站链接应该是带有查询字符串触发器的类似以下内容:

<a href='http://mysite.com?fullsite=true'>Link to full site</a>

然后在您的移动设备检测中:

;(function(a,b) {

    if (document.cookie.indexOf('fullsite') > -1) {
        return; // skip redirect
    }
    if (location.search.indexOf('fullsite') > -1) {
        document.cookie = 'fullsite=true; path=/;'
        return; // skip redirect
    } 
    if (/mobile regex conditional goes here/) {
        window.location = b;
    }
})(navigator.userAgent || navigator.vendor || window.opera, 'http://thefullsiteURL.com/mobile/index.html')

谢谢提供信息!我正在尝试将所有内容整合在一起,但不确定是什么导致了错误。我已经将它放在这个fiddle中: http://jsfiddle.net/ultraloveninja/8KYC5/1/ - ultraloveninja
1
我的原始答案只是勾勒出了逻辑。我已经在上面更新了它,使其完整,现在您应该能够将其放入其中。注意:您需要在最后一个if语句中粘贴冗长的移动检测正则表达式条件。 - chelmerich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接