首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何重定向到移动,但让用户从那时起切换?

我如何重定向到移动,但让用户从那时起切换?
EN

Stack Overflow用户
提问于 2014-08-24 22:54:06
回答 2查看 79关注 0票数 0

我在重定向到我的移动版本时遇到了问题。我没有足够的编程技巧来完成这个任务。我被卡住了。

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

  1. 当移动用户访问移动版本时,默认情况下将服务于移动版本。
  2. 如果他们单击“查看完整站点”,则该站点切换并停留在完整站点上,直到并且只有当他们单击“查看移动版本(页脚)”时,它才会停留在移动版本上,除非他们再次单击“查看完整站点”。

就是这样,但我不能让它起作用。

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

代码语言:javascript
复制
<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>

然后,整个站点上的超链接是:

代码语言:javascript
复制
<a href="/m/mobile.html" onclick="writeCookie('fullsite', 'm')">View Mobile Site</span></a>

同样,移动站点也有自己的链接:

代码语言:javascript
复制
<a href="../index.php" onClick="writeCookie('fullsite', 't')">FULL SITE VERSION</a>

问题:我永远无法让网站(第一次)转到移动版本。它只适用于完整的桌面版本。这是个阻止表演的地方。如果访问者正在移动设备上访问该站点,则必须能够自动将其发送到该移动站点。不需要单击任何内容。

有什么想法吗?在没有帮助的情况下,我将无法解决这个问题。当然,这是我昨天需要的东西。

EN

回答 2

Stack Overflow用户

发布于 2014-08-24 23:09:11

您的else放在if中,它只在非iPhone条件下触发。试试这个:

代码语言:javascript
复制
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 = "/";
}
票数 1
EN

Stack Overflow用户

发布于 2014-08-24 23:27:17

类似于“忠诚度科技”的建议,但我有一些要补充。

代码语言:javascript
复制
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 = "/";
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25477061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档