simple javascript cookie question

A

alex.kemsley

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.
 
E

Evertjan.

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"?
 
A

alex.kemsley

form mail.
As in an email program that sends you the content in a form.
Thank
Alex
 
E

Evertjan.

alex.kemsley wrote on 05 sep 2006 in comp.lang.javascript:
[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?
 
A

Alvaro G. Vicario

*** 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.getUTCMilliseconds()+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)){
return unescape(re.exec(c)[2]);
}
}
return null;
}
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 6
Sep 2006 00:13:20 remote, seen in Alvaro G.
Vicario said:
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.getUTCMilliseconds()+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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top