Cookie problem! Getting error when passing the expire date.

P

paul

HI! I keep on getting this error and I have tried different things but I am
not sure how to send the expiring date.

The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

----------------------------------------------------
I have this in a .js file and in the head section.

function Set_Cookie(name,value,expires,path,domain,secure) {
document.cookie = name + "=" +escape(value) +
( (expires) ? ";expires=" + expires.toGMTString() : "") +
( (path) ? ";path=" + path : "") +
( (domain) ? ";domain=" + domain : "") +
( (secure) ? ";secure" : "");
}


I am calling it like this.

<script type="text/javascript">
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC", "/");
</script>

What Am I doing wrong?

Paul
 
W

web.dev

paul said:
The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

( (expires) ? ";expires=" + expires.toGMTString() : "") + [snip]
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC", "/");

What Am I doing wrong?

The toGMTString() method is method belonging to the Date object. In
your example, you are passing a string "Thu, 2 ...etc". Strings don't
have toGMTString() method.
 
S

Stephen Chalmers

paul said:
HI! I keep on getting this error and I have tried different things but I
am not sure how to send the expiring date.

The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

I am calling it like this.

<script type="text/javascript">
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC", "/");
</script>

What Am I doing wrong?

toGMTString must be called for a Date object not a string literal.
Your parameter should be generated thus:

var now=new Date( new Date().setDate( now.getDate() + 31) ) //31 days

although if the expiry is intended to be after a fixed number of days,
rather
than a specific date, your function would look better accepting an integer
and using it as above to specify the expiry date.
 
P

paul

web.dev said:
paul said:
The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

( (expires) ? ";expires=" + expires.toGMTString() : "") + [snip]
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC",
"/");

What Am I doing wrong?

The toGMTString() method is method belonging to the Date object. In
your example, you are passing a string "Thu, 2 ...etc". Strings don't
have toGMTString() method.

HI! thanks for reponding, hmm, how do I translate the date to numeric? do I
do it like 05, 2 06 2001 20:47:11 ? what about the UTC? do you I use a HEX
value?

Paul
 
W

web.dev

paul said:
The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

( (expires) ? ";expires=" + expires.toGMTString() : "") + [snip]
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC", "/");

What Am I doing wrong?

The toGMTString() method is method belonging to the Date object. In
your example, you are passing a string "Thu, 2 ...etc". Strings don't
have toGMTString() method.
 
P

paul

Stephen said:
toGMTString must be called for a Date object not a string literal.
Your parameter should be generated thus:

var now=new Date( new Date().setDate( now.getDate() + 31) ) //31 days

although if the expiry is intended to be after a fixed number of days,
rather
than a specific date, your function would look better accepting an integer
and using it as above to specify the expiry date.

HI! Thanks, I will have to take a different aproach then:

Paul
 
P

paul

Stephen said:
toGMTString must be called for a Date object not a string literal.
Your parameter should be generated thus:

var now=new Date( new Date().setDate( now.getDate() + 31) ) //31 days

although if the expiry is intended to be after a fixed number of days,
rather
than a specific date, your function would look better accepting an integer
and using it as above to specify the expiry date.

HI! I am tring somthing else and I get the expired date but I am now having
trouble adding the path

function Set_Cookie(cookieName,cookieValue,nDays,cookiePath) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue) +
";expires="+expire.toGMTString(); + "path="+cookiePath";
}

I am using this to call it.

<script type="text/javascript">
Set_Cookie("checkpopupwarning", "1", "7", "/");
</script>

I am getting the following as an error:

"Error: unterminated string literal"

I have tried different things but I am still getting this error or
"cookiePath not defined".

Am I adding this together correctly?


Paul
 
W

web.dev

paul wrote:
[snip]
";expires="+expire.toGMTString(); + "path="+cookiePath";

First, remove the semicolon from the call to expire.toGMTString()
Second, you have one too many quotes.

So it should be more like the following:

";expires=" + expire.toGMTString() + "path=" + cookiePath;
 
P

paul

web.dev said:
paul wrote:
[snip]
";expires="+expire.toGMTString(); + "path="+cookiePath";

First, remove the semicolon from the call to expire.toGMTString()
Second, you have one too many quotes.

So it should be more like the following:

";expires=" + expire.toGMTString() + "path=" + cookiePath;

HI! Great it works, one thing that I dont understand though is why the
forward slash does not need quotes, is it not a string value?

Paul
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu,
27 Apr 2006 22:35:00 remote, seen in Stephen

You are already supplying a string, so toGMTString() is not wanted.

toGMTString must be called for a Date object not a string literal.
Your parameter should be generated thus:

var now=new Date( new Date().setDate( now.getDate() + 31) ) //31 days

Does not work.

now = new Date(+new Date()+31*864e5) // go ahead 31*24 hours

with (now=new Date()) setDate(getDate()+31) // go ahead 31 days

with (now=new Date()) setMonth(getMonth()+1) // go ahead 1 month

with (now=new Date()) setHours(31*24,0,0,0) // start of 31st day

Technically, IIRC, the format specified for cookies is not exactly that
usually given by toGMTString, but it seems not to matter.
 
T

Thomas 'PointedEars' Lahn

web.dev said:
paul wrote:
[snip]
";expires="+expire.toGMTString(); + "path="+cookiePath";

First, remove the semicolon from the call to expire.toGMTString()
Second, you have one too many quotes.
True.

So it should be more like the following:

";expires=" + expire.toGMTString() + "path=" + cookiePath;

It should be more like

"; expires=" + expire.toGMTString() + "; path=" + cookiePath;


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Thu, 27 Apr 2006 18:45:01 remote, seen in
paul said:
function Set_Cookie(cookieName,cookieValue,nDays,cookiePath) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue) +
";expires="+expire.toGMTString(); + "path="+cookiePath";
}


Don't let your posting agent line-wrap your code; for code legibility, I
suggest a space after each comma, a space each side of a concatenating
+, and indenting by 2 (or 3) spaces per level.

You don't need two Date Objects.

If a Date Object is needed, but it does not need to be set to the
current date/time, use new Date(0) for speed and reproducibility.

ISTM that in such a case it's not necessary to check nDays, which will
almost always be supplied as a Number literal. But if it is checked,
ISTM that it should be more fully checked - NaN, +-Infinity, function,
.... .

Since Cookie setting does not need speed, it might be simplest to
convert it to String, check with RegExp, and if OK convert back with
unary +. Alternatively, X|0 seems to convert most things to a
respectable Number.
 
P

paul

Dr said:
JRS: In article <[email protected]>, dated
Thu, 27 Apr 2006 18:45:01 remote, seen in


Don't let your posting agent line-wrap your code; for code legibility, I
suggest a space after each comma, a space each side of a concatenating
+, and indenting by 2 (or 3) spaces per level.

You don't need two Date Objects.

If a Date Object is needed, but it does not need to be set to the
current date/time, use new Date(0) for speed and reproducibility.

ISTM that in such a case it's not necessary to check nDays, which will
almost always be supplied as a Number literal. But if it is checked,
ISTM that it should be more fully checked - NaN, +-Infinity, function,
... .

Since Cookie setting does not need speed, it might be simplest to
convert it to String, check with RegExp, and if OK convert back with
unary +. Alternatively, X|0 seems to convert most things to a
respectable Number.

HI!, Thanks for the responce, I will check out RegExp and convert it.

Thanks again.

Paul
 
E

Evertjan.

Dr John Stockton wrote on 28 apr 2006 in comp.lang.javascript:
Alternatively, X|0 seems to convert most things to a
respectable Number.

+X|0

===========

x='a'
y=+x|0
alert(y);

x='5'
y=+x|0
alert(y);
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 29
Apr 2006 09:33:41 remote, seen in Evertjan.
Dr John Stockton wrote on 28 apr 2006 in comp.lang.javascript:


+X|0

===========

x='a'
y=+x|0
alert(y);

x='5'
y=+x|0
alert(y);

For me, the + makes no visible difference to those.
 
E

Evertjan.

Dr John Stockton wrote on 29 apr 2006 in comp.lang.javascript:
JRS: In article <[email protected]>, dated Sat, 29
Apr 2006 09:33:41 remote, seen in Evertjan.


For me, the + makes no visible difference to those.

You are right.

Because a string, like a zero, equals false, when |-ing.

So if you want only NaN to default to 1, you are in trouble anyway:

x='3'
y=x|1
alert(y); // returns 3, correct

x='aString'
y=x|1
alert(y); // returns 1, correct

x='0'
y=x|1
alert(y); // returns 1,
//"should" return 0, x being convertable to a number.

Not much of a zero tolerance!
 
L

Lasse Reichstein Nielsen

Evertjan. said:
You are right.

Because a string, like a zero, equals false, when |-ing.

More precisely, since "|" is a number operator, not a logical
operator, a non-numeral string converts to NaN.

(For logical comparison, i.e., conversion to boolean, only
the empty string becomes false).

There are a lot of implicit type conversions in
x | 1
where "x" holds a string. More explicitly (using the internal names
for conversion and showing only the ones that makes a difference) it
corresponds to:

toInt32(toNumber(x)) | toInt32(1)

If the string in "x" is not a numeral (as per the definition of
"toNumber"), "toNumber(x)" yields "NaN". Then "toInt32(NaN)"
yields "+0" and "0 | 1" gives 1.

x='0'
y=x|1
alert(y); // returns 1,
//"should" return 0, x being convertable to a number.

And
x='24'
y = x|1;
alert(y); // alerts 25

"Bitwise or" is not a logical guard in the way "logical or" is.
This zero-problem would still be there for || though:
Number("0") || 1
yields 1 as well, since 0 converts to false.

It really should be something like:

function toNumberWithDefault(x,def) {
var n = Number(x);
return isNaN(n) ? def : n;
}

/L
 
E

Evertjan.

Lasse Reichstein Nielsen wrote on 30 apr 2006 in comp.lang.javascript:
And
x='24'
y = x|1;
alert(y); // alerts 25

Wow! Yes.
"Bitwise or" is not a logical guard in the way "logical or" is.
This zero-problem would still be there for || though:
Number("0") || 1
yields 1 as well, since 0 converts to false.

It really should be something like:

function toNumberWithDefault(x,def) {
var n = Number(x);
return isNaN(n) ? def : n;
}


alert(toNumberWithDefault('',7777)) // alerts 0
 
L

Lasse Reichstein Nielsen

Evertjan. said:
Lasse Reichstein Nielsen wrote on 30 apr 2006 in comp.lang.javascript:


alert(toNumberWithDefault('',7777)) // alerts 0

I guess it's a matter of definition whether "" represents a number
(that would be 0 then) or not. The "Number" function appears to think
it does.

If one wants a clearly defined set of number strings to be converted
to numbers, then the format of the string should be tested before
using any standard conversion function, since all standard functions
seem to have the odd case :)

/L
 
V

VK

Lasse said:
I guess it's a matter of definition whether "" represents a number
(that would be 0 then) or not. The "Number" function appears to think
it does.

Not the Number only, but the whole language itself, welcome to the
JavaScript world. Are we aware of runtime typezation? ;-)

alert("" == 0); // true

btw and AFAIK Number is supposed to be used as constructor, not as a
method.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top