cookies... perl.. javascript

Discussion in 'Javascript' started by Lisa, Oct 31, 2003.

  1. Lisa

    Lisa Guest

    Can anyone tell me why the cookie created by this javascript...

    <script language=javascript type="text/javascript">
    <!--
    function SetCookie(username, value, expires, path, domain)
    { document.cookie = username + "=" + escape(value) +
    ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain);
    }
    var expiration = new Date();
    expiration.setTime(expiration.getTime() + 60000);
    SetCookie('username', 'Peter', expiration);
    // -->
    </script>

    is not seen by this perl script?

    #!/usr/local/bin/perl
    use CGI;
    $q = new CGI;
    print $q->header;
    $cookie_in = $q->cookie("username");
    if($cookie_in)
    {
    print $cookie_in;
    }
    else
    {
    print "Can't find cookie\n";
    }

    -Lisa.
    Lisa, Oct 31, 2003
    #1
    1. Advertising

  2. Lisa wrote:

    > Can anyone tell me why the cookie created by this javascript...
    >
    > <script language=javascript type="text/javascript">
    > <!--
    > function SetCookie(username, value, expires, path, domain)
    > { document.cookie = username + "=" + escape(value) +
    > ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
    > ((path == null) ? "" : "; path=" + path) +
    > ((domain == null) ? "" : "; domain=" + domain);
    > }
    > var expiration = new Date();
    > expiration.setTime(expiration.getTime() + 60000);
    > SetCookie('username', 'Peter', expiration);

    ^^^
    > // -->
    > </script>
    >
    > is not seen by this perl script?


    When a named argument of a function is not provided, its
    value is not `null' (since that represents a null, empty,
    or non-existent reference) but `undefined'. So you set the
    cookie's `path' and `domain' to `undefined' as you do not
    provide those arguments. And a site cannot read the cookies
    not of its domain set which explains why your Perl script
    fails.

    In boolean expressions, `undefined' evaluates to `false',
    so you can use the following:

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



    HTH

    PointedEars
    Thomas 'PointedEars' Lahn, Oct 31, 2003
    #2
    1. Advertising

  3. Thomas 'PointedEars' Lahn <> writes:

    > Lisa wrote:


    > > function SetCookie(username, value, expires, path, domain)
    > > { document.cookie = username + "=" + escape(value) +
    > > ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +

    ....
    >
    > When a named argument of a function is not provided, its
    > value is not `null' (since that represents a null, empty,
    > or non-existent reference) but `undefined'. So you set the
    > cookie's `path' and `domain' to `undefined' as you do not
    > provide those arguments.


    However, since Lisa uses "==" to compare, it still works, since
    type conversion makes:
    (undefined == null)
    true.

    (but yes, just using "expires" in the condition is sufficient)

    /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, Oct 31, 2003
    #3
    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. Alex Nitulescu

    Response.Cookies vs Request.Cookies

    Alex Nitulescu, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,451
    Hans Kesting
    Feb 3, 2005
  2. Andy Fish
    Replies:
    3
    Views:
    6,501
    Fredrik Lindner
    Nov 6, 2003
  3. user
    Replies:
    3
    Views:
    647
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Mar 31, 2007
  4. archana
    Replies:
    1
    Views:
    498
  5. _Who
    Replies:
    7
    Views:
    2,629
Loading...

Share This Page