Forum Moderators: open

Message Too Old, No Replies

Help with cookies

I'm trying to figure out how to keep a cookie from being overwritten

         

gilahacker

11:40 pm on Apr 14, 2004 (gmt 0)

10+ Year Member



ok, here goes...

I take some info (referring site particularly) when a visitor enters my site and stick it in a cookie.

When a visitor submits a form on my website, I pull the value from the cookie and send it with the form.

it seems to work fine unless the visitor goes back to the homepage (or even reloads it) as that's where my setcookie function is called. I need to make it so this function won't be called if the cookie's already been set.

i.e. "if cookie doesn't exist then create cookie, if it does exist then do nothing"

this is my setcookie function that gets called when a visitor hits the homepage:

function setcookie()
{
var the_cookie = escape(data);
var the_date = new Date("December 31, 2050");
var the_cookie_date = the_date.toGMTString();
the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie = the_cookie;
}

what i'm thinking is something like:

function setcookie()
{
if document.cookie!="" then
{
var the_cookie = escape(data);
var the_date = new Date("December 31, 2050");
var the_cookie_date = the_date.toGMTString();
the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie = the_cookie;
}
}

I'm not a javascript guru in any sense of the word and I'm not quite sure what I'm doing wrong here but using the blue code, a cookie is never set at all (even if no cookies exist)...

Hoping someone can point me in the right direction.

Thanks In Advance

-Jason
Webmaster, www.cantlistmysiteheresowhybothertheylldeleteitanyway.com

Rambo Tribble

12:18 am on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cookie is a document property, so if you preface your cookie setting code with a conditional testing for its existence (rather than the cookie's equivalence with an empty string) you should be able to prevent duplication. In other words, something like this:

function setCookie(){
if(!document.cookie){
\\cookie creation code
}
}

[edited by: Rambo_Tribble at 3:09 am (utc) on April 15, 2004]

Elijah

12:20 am on Apr 15, 2004 (gmt 0)

10+ Year Member



I think it might work if you use brackets around the if statement.
Like this:

function setcookie()
{
[b]if (document.cookie!="")[/b]
{
var the_cookie = escape(data);
var the_date = new Date("December 31, 2050");
var the_cookie_date = the_date.toGMTString();
the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie = the_cookie;
}
}

Elijah

gilahacker

1:14 am on Apr 15, 2004 (gmt 0)

10+ Year Member



rambo tribble,

I came really close to telling you that your approach didn't work but then realized that I had if!(document.cookie) when I should have had if(!document.cookie)

here's the working code:

function setcookie()
{
if!(document.cookie)
{
var the_cookie = escape(data);
var the_date = new Date("December 31, 2050");
var the_cookie_date = the_date.toGMTString();
the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie = the_cookie;
}
}

THANK YOU RAMBO TRIBBLE

Rambo Tribble

3:55 am on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My pleasure, gilahacker, but I think you posted as working, the typo code.

Conditionals in JavaScript like if(), for(), while(), and switch() always place the conditional test for their execution between the parentheses that follow the function's name. The curly braces, {}, are used to group lines of code to be executed if the conditional test succeeds.

In this case the conditional is "!document.cookie" which translates in R2-D2 as "not-exist-document-cookie?" If the document is found devoid of such apputenance the resulting response from this test is in the affirmative, or in Booleanese, true. A true condition, of course, results in the execution of the code which follows the conditional. Or, more succinctly in our case, "no-cookie-we-goie".

The first three of the conditional functions keep their test mechanics within the parentheses, while switch() spreads it out in the body of the subsequent code to perform its special branching magic.

gilahacker

8:33 am on Apr 15, 2004 (gmt 0)

10+ Year Member



doh! yes I did post the non-working code...

goo... at least the working code is up and running on my website... (has been working quite nicely actually)

Thanks Again

-JL