我如何重定向到移动设备,但让用户从那时开始切换

How do I redirect to mobile, but let the user switch from then on?

本文关键字:用户 从那时 开始 重定向 移动      更新时间:2023-09-26

我遇到了一个问题,重定向到我的移动版本。我没有编程技能来完成这个任务。我卡住了。

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

  1. 手机用户访问时,默认使用手机版本。
  2. 如果他们点击"查看完整的网站",然后它切换并停留在完整的网站,直到,只有如果他们点击查看移动版本(页脚),然后它停留在移动版本,除非他们再次点击"查看完整的网站"。

就是这样,但是我不能使它工作。

下面是我放在头文件中的代码:

<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 = "/";
}

类似于@Loyalty Tech的建议,但我有一些要补充。

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 = "/";
}