[英]My cookie isn't working when I revisit the page
When people type in my URL, I want them to click North Carolina or Virginia. 当人们输入我的URL时,我希望他们单击北卡罗来纳州或弗吉尼亚州。 After they click, it redirects them to either www.myurl.com/nc or www.myurl.com/va. 单击后,它将它们重定向到www.myurl.com/nc或www.myurl.com/va。 After they answer that question, next time they visit my site, it will redirect them to the appropriate page without clicking NC or VA again. 他们回答该问题后,下次他们访问我的网站时,它将重定向到相应的页面,而无需再次单击NC或VA。 My links are working (taking me to the correct page) but when I return, the cookie is not redirecting me and i'm returning to the same page where I ask to choose the state. 我的链接有效(将我带到正确的页面),但是当我返回时,Cookie不会重定向我,并且我将返回到我要求选择状态的同一页面。 Here's what I've got so far. 这是到目前为止我得到的。 I feel like I'm close, but I honestly have no clue what I'm doing. 我感觉自己已经接近了,但老实说我不知道自己在做什么。
<p> Please tell us where you live so we can give you information for your location. </p>
<a href="#" onClick="window.location = 'http://www.myurl.com/nc'" value="nc">North Carolina</a><br />
<br />
<a href="#" onClick="window.location = 'http://www.myurl.com/va'" value="va">Virginia</a>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
function redirect(state) { createCookie('state', state, 90); window.location.href = "http://www.myurl.com/" + nc; }
</script>
I also have this script in a separate file: 我在单独的文件中也有此脚本:
// JavaScript Document
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
Try this: 尝试这个:
<p> Please tell us where you live so we can give you information for your location. </p>
<a href="#" onClick="redirect('nc');return false" >North Carolina</a><br />
<br />
<a href="#" onClick="redirect('va');return false" >Virginia</a>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
function redirect(state) {
createCookie('state', state, 90);
window.location.href = "http://www.myurl.com/" + state;
}
var cookie = readCookie('state');
if (cookie != null) {
window.location.href = "http://www.myurl.com/" + cookie;
}
</script>
jsfiddle: http://jsfiddle.net/7j8pG/1/ jsfiddle: http : //jsfiddle.net/7j8pG/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.