javascript - 带有完整站点链接的移动站点重定向

标签 javascript redirect mobile

我正在使用来自 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 技能相当新,所以如果有人有更优雅的解决方案,我会全力支持。

最佳答案

在您的完整站点链接中设置 session 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')

关于javascript - 带有完整站点链接的移动站点重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12144775/

相关文章:

javascript - 如何解决 找不到部分车头 Handlebars

redirect - 带有 HTTP 和 HTTPS 但没有重定向的 Nginx

iphone - 您将如何根据运营商重定向移动网页?

javascript - 如何使用抛物线方程绘制图形

javascript - wami-recorder - Wami.startRecording 不是一个函数

http - 将站点重定向到其他站点的推荐方法是什么?

ruby-on-rails - 使用 Mechanize Ruby 检测重定向到特定 IP

mobile - GoCD 部署到 Crashlytics Android 和 IOS

java - toast 不会消失

javascript - Three.js 点击事件后沿路径移动相机