Javascript 如何重定向到mobile,但让用户从此切换?

Javascript 如何重定向到mobile,但让用户从此切换?,javascript,jquery,html,redirect,mobile,Javascript,Jquery,Html,Redirect,Mobile,我在重新定向到我的移动版本时遇到了问题。我没有编程技能来完成这项工作。我被卡住了 下面是我的代码,但首先是我想要完成的逻辑: 当移动用户访问时,默认情况下会提供移动版本 如果他们单击“查看完整站点”,则它将切换并保持在完整站点上,直到并且仅当他们单击查看移动版本(页脚),然后它保持在移动版本上,除非他们再次单击“查看完整站点” 就是这样,但我不能让它工作 下面是我放在标题中的代码: <script type="text/javascript"> <!-- var fullSit

我在重新定向到我的移动版本时遇到了问题。我没有编程技能来完成这项工作。我被卡住了

下面是我的代码,但首先是我想要完成的逻辑:

  • 当移动用户访问时,默认情况下会提供移动版本
  • 如果他们单击“查看完整站点”,则它将切换并保持在完整站点上,直到并且仅当他们单击查看移动版本(页脚),然后它保持在移动版本上,除非他们再次单击“查看完整站点”
  • 就是这样,但我不能让它工作

    下面是我放在标题中的代码:

    <script type="text/javascript">
    <!--
    var fullSiteCookie = readCookie('fullsite');
    if ( fullSiteCookie !='t' ) {
      if (screen.width <= 600) {
        window.location = "/m/mobile.html";
      }
      if ( (navigator.userAgent.indexOf('Android') != -1) ) {
        document.location = "/m/mobile.html";
      } 
      if ( (navigator.userAgent.indexOf('iphone') != -1) ) {
        document.location = "/m/mobile.html";
      }
    else
      document.location="/";
    }
    //-->
    </script>
    
    
    
    那么完整站点上的超链接是:

    <a href="/m/mobile.html" onclick="writeCookie('fullsite', 'm')">View Mobile Site</span></a>
    
    
    
    同样,移动站点也有自己的链接:

    <a href="../index.php" onClick="writeCookie('fullsite', 't')">FULL SITE VERSION</a>
    
    
    
    问题:我无法让网站(第一次)转到移动版本。它只进入完整的桌面版本。那是一场表演。如果访问者通过移动设备访问该网站,它必须能够自动将访问者发送到该移动网站。用户不必单击任何内容


    有什么想法吗?如果没有帮助的话,我是无法解决这个问题的。当然,这是我昨天需要的东西。

    您的
    else
    被放置在if中,因此它仅在非iPhone条件下触发。 试试这个:

    if ( fullSiteCookie !='t' ) {
        if (fullSiteCookie =='m' || screen.width <= 600 || navigator.userAgent.indexOf('Android') != -1 || navigator.userAgent.indexOf('iphone') != -1) {
            window.location = "/m/mobile.html";
        } else {
            // not mobile or forced mobile
            document.location = "/";
        }
    
    } else {
        // forced Normal 
        document.location = "/";
    }
    
    if(fullSiteCookie!='t'){
    
    if(fullSiteCookie=='m'| | screen.width类似于@忠诚度技术公司的建议,但我还有一些补充

    var fullSiteCookie = readCookie('fullsite');
      /* Verify that "fullSiteCookie" is a valid value.
         Are you sure the cookie is set the first time?
      */
    if ( fullSiteCookie !='t' && 
       (screen.width <= 600 ||
       navigator.userAgent.indexOf('Android') != -1 ||
    /* Fix case of "iphone" to "iPhone" */
       navigator.userAgent.indexOf('iPhone') != -1)){
        window.location = "/m/mobile.html";
    } else {
      window.location = "/";
    }
    
    var fullsitecokie=readCookie('fullsite');
    /*验证“fullSiteCookie”是否为有效值。
    你确定第一次设置了cookie吗?
    */
    如果(fullSiteCookie!=“t”&&
    
    (screen.width您应该在服务器端执行此操作,否则如果JavaScript不可用,它将失败。至于您的方法,我将查看if语句中的实际值,而不是假设它们是您所期望的(
    console.log()
    是您的朋友)。另外,将您的上一个用户代理更改为
    iPhone
    ,区分大小写。是的,我想对您的用户代理进行评论,但我的if语句偏好不同。请描述使用window.location=vs.document.location=这是一个错误,我的意思是
    window.location
    。仍然存在问题。我很简单如果您是第一次使用移动设备,或者处于“匿名”浏览器会话中,y无法确定如何将网站强制转换为移动版本。默认情况下,它不会第一次转到移动设备。在您单击查看移动版本或单击返回桌面版本后,一切都很好。是否有必要强制将cookie转换为be设置(如果在移动设备上)首先,fullSiteCookie的值为'm',然后移动站点将首先出现。这似乎可行,但现在我发现,如果未设置cookie,它仍然无法识别。这使得它总是命中桌面站点,而不是预期的移动站点。一旦你到达移动站点,则来回工作正常。如果有一种方法可以检查引用页面,看看它是否来自移动页面?@NathanPizzo检查控制台是否有错误,并在
    readCookie
    之后插入
    console.log(fullSiteCookie)
    ,看看该值是否符合您的预期。