简体   繁体   English

当我重新访问页面时,我的cookie无法正常工作

[英]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.

相关问题 当我再次访问该页面时,为什么我的字体大小会发生变化 - Why does my font-size change when I revisit the page Javascript-检查页面加载时是否存在cookie对我不起作用 - Javascript - checking if cookie exists on page load isn't working for me 当我发布到页面时,javascript函数不起作用 - When I POST to a page, javascript function isn't working 为什么我使用 css 时我的页面没有加载? - Why isn't my page loading when I use css? 当我运行我的节点 js 应用程序时,cookie 没有保存在 Chrome 浏览器中 - When I run my node js application the cookie isn't saved in Chrome browser 当我使用 fetch my.then 代码不起作用 - When I use fetch my .then code isn't working $ locationProvider无法在我的Angular页面中使用 - $locationProvider isn't working in my Angular page 我无法弄清为什么此Cookie测试无法正常工作 - I cannot work out why this cookie test isn't working 如何让我的密码提示只在我打开我的网站时出现一次,而不是每次我重新加载页面或重新访问它时出现 - How do I make my password prompt only show up once when I open up my website instead of every time I reload the page or revisit it 当把它放在index.hbs页面上时,把手变量起作用,但是当我将其与<script> - Handlebars variable working when it placed on index.hbs page, but isn't working, when I connect it with <script>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM