Sunday, February 7, 2010

Set Cookie with Variable Using Eval in Javascript

Have you ever encountered a situation that you need to set a cookie with a javascript variable added to a cookie? But you cannot use "+" in your cookie assignment?

Here is where the javascript eval() comes into play:


function assignfruit(type) {
eval('document.cookie = "fruit='+ type +';"');
}


Since you cannot do this: document.cookie = "fruit="+ type +";" or this: document.cookie = 'fruit='+ type +';', you need to use eval().


Something I found out and I think it is useful to beginners to understand eval().


No comments:

Post a Comment