仅重定向移动网站一次

时间:2013-01-10 15:12:37

标签: html redirect mobile

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

我无法发布我找到的代码,但问题是全部

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

我没有专业经验......只是初学者

2 个答案:

答案 0 :(得分:0)

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

如何在PHP中设置Cookie herehttp://www.w3schools.com/php/php_cookies.asp

答案 1 :(得分:0)

按照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也会如此。