简体   繁体   English

使用Cookie将用户重定向到移动网站以进行检查

[英]Redirect users to mobile site using cookie to check

I have a script in my html code (main page): 我的html代码(主页)中有一个脚本:

  <script type="text/javascript">
   <!--
     if (screen.width <= 550) {
     window.location.href = "http://EXAMPLE/chooice.html";
          }
  //-->
  </script>

Ok i'ts plain and simple. 好吧,我不是很简单。 redirect the user to the chooice.html whenever you are under the 550px area. 只要您位于550px以下区域,就将用户重定向到chooice.html。 Cool. 凉。 Works.. 作品..

Now here is the problem. 现在这是问题所在。 I have setup chooice.html to have two buttons ( one of them says go to mobile site, the other one says go to main website ). 我已将chooice.html设置为具有两个按钮( 其中一个说进入移动网站,另一个说进入主网站 )。 So my button that says go to main website is were the script is. 所以我说去主要网站的按钮是脚本。 So if I as a user chooice the regular site my script will trigger it once again and I will go back to the chooice.html site.... Fail! 因此,如果我作为用户选择常规站点,则我的脚本将再次触发该脚本,然后我将返回chooice.html站点。...失败!

I started thinking that I can use a cookie for this to check if it needs a redirect or not? 我开始认为我可以为此使用cookie来检查它是否需要重定向? You guys get me? 你们弄我吗? I don't know too much javascript so if anyone could help me that would be awesome but I think a cookie is the way to go at this point. 我不太了解javascript,所以如果有人可以帮助我,那真是太棒了,但我认为cookie是目前解决方案。

Thanks 谢谢

You could use Javascript cookies. 您可以使用Javascript Cookie。

To write to them, use the following formats 要写给他们,请使用以下格式

document.cookie="item=value";
document.cookie="item=value; expires=Mon, 1 Jan 2014 00:00:00 UTC";
document.cookie="item=value; expires=Mon, 1 Jan 2014 00:00:00 UTC; path=/";

You can then read it from the following item 然后可以从以下项目中阅读

var cookie = document.cookie;

If you need anymore information, take a look at the following link: http://www.w3schools.com/js/js_cookies.asp 如果您需要更多信息,请查看以下链接: http : //www.w3schools.com/js/js_cookies.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM