【问题标题】:redirect a mobile site only once仅将移动网站重定向一次
【发布时间】:2013-01-10 23:41:59
【问题描述】:

我只想知道要粘贴到我网站的“index.html”的代码,这样我就可以询问手机用户是否希望被重定向到移动版或桌面版 但我还没想出办法 因为我发现的代码总是重定向,没有机会在移动设备上转到桌面版本

我无法发布我找到的代码,但问题说明了一切

如果这不可能,我只想知道一个仅将移动用户重定向“ONCE”的代码,谢谢

我没有这方面的专业经验......只是一个初学者

【问题讨论】:

  • @jonhopkins 该链接对他来说几乎是完美的,可能想建议它作为答案。
  • @jonhopkins 您提供的代码是否只重定向一次?我必须将手机/移动网站更改为我的移动网站吗?
  • @gs2rom 我不确定。我刚刚找到它是因为你的问题让我想做同样的事情。我现在正在查看它,以确定它是否存在。

标签: html redirect mobile


【解决方案1】:

按照http://www.ezmobilewebsitetools.com/howto-redirect.html 的说明进行操作:

将以下代码粘贴到 index.html 的 <head> 部分

<script>
if ((document.location.hostname.match(/\.mobi$/) || screen.width < 699)
    && (document.cookie.indexOf("skipmobile") == -1 || getCookie("skipmobile") == -1)
{
    document.location = "mobile/"; //change this to location of your mobile site
} else if (document.location.search.indexOf("skipmobile") >= 0) {
    document.cookie = "skipmobile=1";
}

//getCookie function from http://www.w3schools.com/js/js_cookies.asp
function getCookie(c_name) {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++) {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name) {
            return unescape(y);
        }
    }
}
</script>

这将检测用户是否使用屏幕宽度小于 699 像素的设备,或者他们是否访问了您网站的 .mobi 域(如果您有的话)。要允许用户返回桌面网站,请在您的移动页面上的某处创建一个链接

<a href="http://www.MyWebsite.com/?skipmobile=1">

这将创建一个 cookie,通知该网站继续为该用户留在桌面网站上。基于相同的逻辑,我相信将以下链接放在桌面网站上会更改此 cookie 以允许用户返回移动网站。

<a href="http://www.MyWebsite.com/?skipmobile=-1">

请注意,我对 cookie 了解不多,也没有对此进行测试。如果有更多知识的人可以验证或纠正第二个链接,我将不胜感激,我相信 gs2rom 也会。

【讨论】:

【解决方案2】:

您可以使用 Cookie。例如,当用户选择“桌面版”时设置一个Cookie,当用户返回时检查它是否存在。

如何在 PHP 中设置 Cookie here (http://www.w3schools.com/php/php_cookies.asp)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    相关资源
    最近更新 更多