Cookie问题 [英] Cookie question

查看:80
本文介绍了Cookie问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里学习JavaScript并做错了什么,我想要的是
做的是将变量的内容存储到cookie中以便它将显示下一个
页面打开的时间。我在下面的代码中做了一些错误的事情,因为我没有将nLastBonus的值保存到

,而是将cookie保存为nLastBonus。 />

有人能指出我正确的方向吗?


//获取cookie

var nLastBonus = get_cookie()


//饼干是否存在?

if(nLastBonus){


//如果没有,那么商店100到nLastBonus

// visit_number = 1

nLastBonus = 100

}

else {


//否则,增加nLastBonus

nLastBonus ++

}


//设置cookie

document.cookie =" last_bonus = nLastBonus;期满= QUOT; + expire_string

解决方案

ra ***** @ icubed .com (Jim Davidson)写道:

我在下面的代码中做错了,因为而不是将nLastBonus的值保存到cookie中我正在保存
nLastBonus。


我假设其余的工作。更改:

document.cookie =" last_bonus = nLastBonus;期满= QUOT; + expire_string



to

document.cookie =" last_bonus =" + escape(nLastBonus)" +; expires =" + expire_string;


通常,你应该转义存储在cookie中的值,以确保它们不包含有意义的字符。
它们不包含有意义的字符。在这种情况下,它不是必须的b $ b,因为数字是安全的,但如果nLastBonus是字符串

" foo; expires = now;" (或类似的东西)然后你会得到

麻烦。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

Art D''HTML:< URL: http://www.infimum.dk/HTML/randomArtSplit.html>

''没有判断的信仰只会降低精神神圣。''


< blockquote>

" Lasse Reichstein Nielsen" < lr*@hotpop.com> schreef in bericht

news:vf ********** @ hotpop.com ...


document.cookie =" last_bonus =" + escape(nLastBonus)" +;
expires =" + expire_string;




当您在上面的行中出现错误时,请将其更改为:


document.cookie =" last_bonus =" + escape(nLastBonus)+" ; expires =" + expire_string;

JW


" Janwillem Borleffs" < jw@jwscripts.com>在消息新闻中写道:< 3f ********************** @ news.euronet.nl> ...

" Lasse Reichstein Nielsen < lr*@hotpop.com> schreef in bericht
新闻:vf ********** @ hotpop.com ...


document.cookie =" last_bonus =" + escape(nLastBonus)" +;


expires =" + expire_string;



当你收到错误时上面一行,将其改为:

document.cookie =" last_bonus =" + escape(nLastBonus)+" ; expires =" + expire_string;

JW



谢谢那些有效的人!


I learning JavaScript and doing something wrong here, what I want to
do is store the contents of a variable to a cookie so that it would
show the next time the page was opened. I''m doing something wrong in
the code below because instead of saving the value of nLastBonus to
the cookie I''m saving "nLastBonus".

Can someone point me in the right direction?

// Get the cookie
var nLastBonus = get_cookie()

// Did the cookie exist?
if (nLastBonus) {

// If not, then store 100 to nLastBonus
// visit_number = 1
nLastBonus = 100
}
else {

// Otherwise, increment nLastBonus
nLastBonus++
}

// Set the cookie
document.cookie="last_bonus=nLastBonus; expires=" + expire_string

解决方案

ra*****@icubed.com (Jim Davidson) writes:

I''m doing something wrong in the code below because instead of
saving the value of nLastBonus to the cookie I''m saving
"nLastBonus".
I assume the rest works. Change:
document.cookie="last_bonus=nLastBonus; expires=" + expire_string


to
document.cookie="last_bonus="+escape(nLastBonus)"+ ; expires="+expire_string;

Generally, you should escape the values stored in cookies to make sure
they don''t contain meaningfull characters. In this case, it isn''t
necessary, since numbers are safe, but if nLastBonus was the string
"foo;expires=now;" (or something similar that works) then you will get
trouble.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D''HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
''Faith without judgement merely degrades the spirit divine.''



"Lasse Reichstein Nielsen" <lr*@hotpop.com> schreef in bericht
news:vf**********@hotpop.com...


document.cookie="last_bonus="+escape(nLastBonus)"+ ; expires="+expire_string;



And when you get an error on the above line, change it into:

document.cookie="last_bonus="+escape(nLastBonus)+" ; expires="+expire_string;
JW


"Janwillem Borleffs" <jw@jwscripts.com> wrote in message news:<3f**********************@news.euronet.nl>...

"Lasse Reichstein Nielsen" <lr*@hotpop.com> schreef in bericht
news:vf**********@hotpop.com...


document.cookie="last_bonus="+escape(nLastBonus)"+ ;


expires="+expire_string;



And when you get an error on the above line, change it into:

document.cookie="last_bonus="+escape(nLastBonus)+" ; expires="+expire_string;
JW


Thanks guys that worked!


这篇关于Cookie问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆