移动网站重定向与完整的网站链接

ult*_*nja 4 javascript mobile redirect

我正在使用http://detectmobilebrowsers.com/中的javascript版本重定向到移动网站.唯一的事情就是我有一个链接,可以在用户想要/需要去的地方找到完整的网站.

但是,当您单击链接以从移动设备查看完整站点时,它会重新选择重定向并将其恢复到移动版本而不是完整站点.

我正在做一些搜索,并想知道是否有可能破解它以便它使用

window.location.href.indexOf
Run Code Online (Sandbox Code Playgroud)

或者像这样的那种性质:

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')};
Run Code Online (Sandbox Code Playgroud)

保持在中间,这是我拼凑在一起的东西,我的JS技能是相当新的,所以如果任何人有一个更优雅的解决方案我都是为了它.

che*_*ich 6

在完整站点链接中将会话cookie与查询字符串值一起设置.然后,让您的移动检测代码首先检查cookie值,第二个检查查询字符串,最后检查用户代理移动检测.

因此,您的完整站点链接应该与查询字符串触发器类似:

<a href='http://mysite.com?fullsite=true'>Link to full site</a>
Run Code Online (Sandbox Code Playgroud)

然后在您的移动检测中:

;(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')
Run Code Online (Sandbox Code Playgroud)