Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > cookies... perl.. javascript

Reply
Thread Tools

cookies... perl.. javascript

 
 
Lisa
Guest
Posts: n/a
 
      10-31-2003
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.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-31-2003
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
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      10-31-2003
Thomas 'PointedEars' Lahn <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generated JavaScript vs. Manually Created JavaScript: Which one comes first? Nathan Sokalski ASP .Net 4 11-08-2007 07:24 AM
JavaScript or not JavaScript Mark Rae ASP .Net 36 09-09-2006 01:12 PM
href="javascript:func()" vs href="#" onclick="javascript:func()" CRON HTML 24 06-20-2006 08:05 PM
Javascript -> Applet: variable reset after method call from javascript?! mcdeveloper Java 1 06-13-2006 08:34 AM
To Javascript, or not to Javascript Shane NZ Computing 5 08-30-2005 09:33 PM



Advertisments