Cookie problem! Getting error when passing the expire date.

Discussion in 'Javascript' started by paul, Apr 27, 2006.

  1. paul

    paul Guest

    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
    paul, Apr 27, 2006
    #1
    1. Advertising

  2. paul

    web.dev Guest

    Re: Cookie problem! Getting error when passing the expire date.

    paul wrote:
    > 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.
    web.dev, Apr 27, 2006
    #2
    1. Advertising

  3. "paul" <> wrote in message
    news:1S84g.1806$...
    > 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.

    --
    S.C.
    Stephen Chalmers, Apr 27, 2006
    #3
  4. paul

    paul Guest

    Re: Cookie problem! Getting error when passing the expire date.

    web.dev wrote:
    > paul wrote:
    >> 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
    paul, Apr 27, 2006
    #4
  5. paul

    web.dev Guest

    Re: Cookie problem! Getting error when passing the expire date.

    paul wrote:
    > 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.
    web.dev, Apr 27, 2006
    #5
  6. paul

    paul Guest

    Stephen Chalmers wrote:
    > "paul" <> wrote in message
    > news:1S84g.1806$...
    >> 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.


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

    Paul
    paul, Apr 27, 2006
    #6
  7. paul

    paul Guest

    Stephen Chalmers wrote:
    > "paul" <> wrote in message
    > news:1S84g.1806$...
    >> 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.


    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
    paul, Apr 27, 2006
    #7
  8. paul

    web.dev Guest

    Re: Cookie problem! Getting error when passing the expire date.

    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;
    web.dev, Apr 28, 2006
    #8
  9. paul

    paul Guest

    Re: Cookie problem! Getting error when passing the expire date.

    web.dev wrote:
    > 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
    paul, Apr 28, 2006
    #9
  10. Cookie problem! Getting error when passing the expire date.

    JRS: In article <>, dated Thu,
    27 Apr 2006 22:35:00 remote, seen in news:comp.lang.javascript, Stephen
    Chalmers <> posted :
    >"paul" <> wrote in message
    >news:1S84g.1806$...
    >> 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?


    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.

    --
    © 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.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Apr 28, 2006
    #10
  11. Re: Cookie problem! Getting error when passing the expire date.

    web.dev wrote:

    > 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
    --
    The German psychs, the German authorities, the German secret service agents
    are [...] fanatics, they are insane and known of persecuting innocent people
    and Scientologists. -- "The only real Barbara Schwarz", dsw.scientology,
    <>
    Thomas 'PointedEars' Lahn, Apr 28, 2006
    #11
  12. Cookie problem! Getting error when passing the expire date.

    JRS: In article <WPb4g.1941$>, dated
    Thu, 27 Apr 2006 18:45:01 remote, seen in news:comp.lang.javascript,
    paul <> posted :
    >
    >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.

    --
    © 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.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Apr 28, 2006
    #12
  13. paul

    paul Guest

    Re: Cookie problem! Getting error when passing the expire date.

    Dr John Stockton wrote:
    > JRS: In article <WPb4g.1941$>, dated
    > Thu, 27 Apr 2006 18:45:01 remote, seen in news:comp.lang.javascript,
    > paul <> posted :
    >>
    >> 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.


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

    Thanks again.

    Paul
    paul, Apr 29, 2006
    #13
  14. paul

    Evertjan. Guest

    Re: Cookie problem! Getting error when passing the expire date.

    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);

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Apr 29, 2006
    #14
  15. Re: Cookie problem! Getting error when passing the expire date.

    JRS: In article <Xns97B4759D23B5eejj99@194.109.133.242>, dated Sat, 29
    Apr 2006 09:33:41 remote, seen in news:comp.lang.javascript, Evertjan.
    <> posted :
    >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);


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

    --
    © 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.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Apr 29, 2006
    #15
  16. paul

    Evertjan. Guest

    Re: Cookie problem! Getting error when passing the expire date.

    Dr John Stockton wrote on 29 apr 2006 in comp.lang.javascript:

    > JRS: In article <Xns97B4759D23B5eejj99@194.109.133.242>, dated Sat, 29
    > Apr 2006 09:33:41 remote, seen in news:comp.lang.javascript, Evertjan.
    > <> posted :
    >>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);

    >
    > 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!

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Apr 30, 2006
    #16
  17. Re: Cookie problem! Getting error when passing the expire date.

    "Evertjan." <> writes:

    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Apr 30, 2006
    #17
  18. paul

    Evertjan. Guest

    Re: Cookie problem! Getting error when passing the expire date.

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

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


    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



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Apr 30, 2006
    #18
  19. Re: Cookie problem! Getting error when passing the expire date.

    "Evertjan." <> writes:

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


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

    >
    >
    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Apr 30, 2006
    #19
  20. paul

    VK Guest

    Re: Cookie problem! Getting error when passing the expire date.

    Lasse Reichstein Nielsen wrote:
    > 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.
    VK, Apr 30, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joey Powell

    Forms Authentication Cookie Does Not Expire

    Joey Powell, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    492
  2. Big E

    Cookie Expire

    Big E, Aug 30, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,049
    Scott M.
    Aug 31, 2004
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,250
    Michael Borgwardt
    May 30, 2004
  4. What-a-Tool

    Cookie Expire Date question

    What-a-Tool, Mar 4, 2005, in forum: ASP General
    Replies:
    7
    Views:
    248
    Evertjan.
    May 2, 2005
  5. Tongass Park Neighborhood Association, Juneau Alas

    Cookies expire immediately, not when set to expire

    Tongass Park Neighborhood Association, Juneau Alas, Oct 1, 2009, in forum: ASP General
    Replies:
    2
    Views:
    1,198
    SQLDude
    Nov 24, 2009
Loading...

Share This Page