Forum Moderators: open
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
function setCookie(){
if(!document.cookie){
\\cookie creation code
}
}
[edited by: Rambo_Tribble at 3:09 am (utc) on April 15, 2004]
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
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
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.