By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

simple javascript cookie question

P: n/a
Hi guys,

I am trying to write a simple script in vain.
I need it to first check to see if the cookie exists
then if not
write one with two variable

var ref = document.referrer
var page = location.href

I need this cookie to last just one day.

This script is going to be used to send me this info in my formail so i
can see how people got to my website and where they entered.

Many thanks

Alex
Hot Tubs 2 Buy.co.uk

Your help would be much apreciated.

Sep 5 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
alex.kemsley wrote on 05 sep 2006 in comp.lang.javascript:
I am trying to write a simple script in vain.
I need it to first check to see if the cookie exists
then if not
write one with two variable

var ref = document.referrer
var page = location.href

I need this cookie to last just one day.

This script is going to be used to send me this info in my formail so i
can see how people got to my website and where they entered.
How can we know the script is so simple
if you do not show us wat you wrote?

What is a "formail"?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 5 '06 #2

P: n/a
form mail.
As in an email program that sends you the content in a form.
Thank
Alex

Evertjan. wrote:
alex.kemsley wrote on 05 sep 2006 in comp.lang.javascript:
I am trying to write a simple script in vain.
I need it to first check to see if the cookie exists
then if not
write one with two variable

var ref = document.referrer
var page = location.href

I need this cookie to last just one day.

This script is going to be used to send me this info in my formail so i
can see how people got to my website and where they entered.

How can we know the script is so simple
if you do not show us wat you wrote?

What is a "formail"?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 5 '06 #3

P: n/a
alex.kemsley wrote on 05 sep 2006 in comp.lang.javascript:
Evertjan. wrote:
>alex.kemsley wrote on 05 sep 2006 in comp.lang.javascript:
I am trying to write a simple script in vain.
I need it to first check to see if the cookie exists
then if not
write one with two variable

var ref = document.referrer
var page = location.href

I need this cookie to last just one day.

This script is going to be used to send me this info in my formail
so i can see how people got to my website and where they entered.

How can we know the script is so simple
if you do not show us wat you wrote?

What is a "formail"?
[please do not toppost on usenet]
form mail.
As in an email program that sends you the content in a form.
Is that a clientside thing? Or do you simply send the daa to servrside to
be processed, and if so how?

Because you did toppost,
you did not consider my first and far more important question:
What code did you come up with?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 5 '06 #4

P: n/a
*** alex.kemsley escribió/wrote (5 Sep 2006 07:13:54 -0700):
I am trying to write a simple script in vain.
I need it to first check to see if the cookie exists
then if not
write one with two variable
[...]
I need this cookie to last just one day.
I compiled these functions some time ago, I hope it helps. (I've just translated the names from Spanish using Search & Replace, I hope I didn't break it)
/*
* Creates a cookie - name [value] [timeToExpire (s)] [path] [domain] [secure (bool)]
*/
function createCookie(name, value, timeToExpire, path, domain, secure){ // v2005-03-02
if(timeToExpire){
var expires=new Date();
expires.setUTCMilliseconds(expires.getUTCMilliseco nds()+1000*timeToExpire);
}

document.cookie=escape(name) + '=' + (value? escape(value) : '') +
(expires? '; expires=' + expires.toGMTString() : '') +
(path? '; path=' + escape(path) : '') +
(domain? '; domain=' + escape(domain) : '') +
(secure? '; secure' : '');
}
/*
* Removes a cookie
*/
function removeCookie(name){ // v2005-03-02
createCookie(name, '', -86400*365*10);
}
/*
* Returns the value of a cookie (null if it doesn't exist)
*/
function readCookie(name){ // v2005-02-27
eval('var re=/^('+escape(name)+')=(.*)$/i;');
var c=document.cookie.split(/;\s*/);

for(var i in c){
if(re.test(c[i])){
return unescape(re.exec(c[i])[2]);
}
}
return null;
}

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 5 '06 #5

P: n/a
JRS: In article <v1**************************@40tude.net>, dated Wed, 6
Sep 2006 00:13:20 remote, seen in news:comp.lang.javascript, Alvaro G.
Vicario <we*******@NOSPAMdemogracia.composted :
>I need this cookie to last just one day.

I compiled these functions some time ago, I hope it helps. (I've just translated
the names from Spanish using Search & Replace, I hope I didn't break it)
If you had likewise replaced Tab with Space Space, it would have been
easier to read.

var expires=new Date();
expires.setUTCMilliseconds(expires.getUTCMilliseco nds()+1000*tim
eToExpire);
That has (FWIW) an error of the present fraction of a second, and ISTM
may use conversions to/from YMD internally.

expires.setTime( +expires + 1000*timeToExpire )
or just
expires=new Date( +new Date() + 1000*timeToExpire )
>function removeCookie(name){ // v2005-03-02
createCookie(name, '', -86400*365*10);
}
Is it necessary to go 10 years ago?

eval('var re=/^('+escape(name)+')=(.*)$/i;');
Function eval should not be needed.

It's a good idea to read the newsgroup and its FAQ.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 6 '06 #6

P: n/a
"alex.kemsley" <al**********@hottubs2buy.co.ukwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi guys,

I am trying to write a simple script in vain.
I need it to first check to see if the cookie exists
then if not
write one with two variable

var ref = document.referrer
var page = location.href

I need this cookie to last just one day.

This script is going to be used to send me this info in my formail so i
can see how people got to my website and where they entered.
I've a cookie abstraction library that might help here:

http://www.depressedpress.com/Conten...kies/Index.cfm

There is one sorta bug in this version (which I hope to fix tomorrow): the
erase() method doesn't accept a path or domain which means it will fail with
cookies which set path and domain (instead of deleting the named cookie it
will create a second cookie without the path or domain).

If you're not doing that then it should work, if you are don't use the
erase() method for now and instead reset the cookie (the set() method) to a
past date to delete it.

Jim Davis
Sep 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.