Vertigo has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to execute a javascript that downloads an image and the store the results in a cookie. The problem is I acant execute the script before i set the cookies

here is what i have:

---(trying to put javascript here)---

@nvpairs=split(/; /, $ENV{'HTTP_COOKIE'}); foreach $pair (@nvpairs) { ($name, $value) = split(/=/, $pair); $cookie{$name} = $value;} if ($cookie{'visited'} eq ""){print "Set-Cookie: speed=$speed;\n\n";}else{print "Content-type:text/html\n\n";}

Edit by castaway - added code tags

Replies are listed 'Best First'.
Re: Execute javascript before setting cookie headers
by talexb (Chancellor) on Sep 27, 2005 at 20:40 UTC

    I'm having trouble parsing what you are trying to do.

    When a web server sends out a page in response to a browser's request, it includes a header (which can include cookie information, among other things) and the body (which includes JavaScript). The JavaScript is run by the browser, but AFAIK can't touch the cookies. The body may include image tags, which the browser can choose to follow by asking the server for the corresponding image files.

    What are you trying to accomplish? Tell us that, and we can probably suggest some ways to do it.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

    Update: Corion tells me that JavaScript can indeed touch that domain's cookies. Cool.

Re: Execute javascript before setting cookie headers
by InfiniteSilence (Curate) on Sep 27, 2005 at 21:57 UTC

      I think it's not what the OP is asking but you could reliably store small images (<4kb) in cookies (might have to encode certain chars like line endings?) and I suspect some browsers might even accept larger ones. You could also break up a bigger image across a hundred cookies. I embedded a bunch of PNGs written out in ASCII inside a polling application module so that the client would have no chance of losing them.

Re: Execute javascript before setting cookie headers
by cfreak (Chaplain) on Sep 28, 2005 at 00:19 UTC

    As it was pointed out already, you can't save an image in a cookie. You can preload images without displaying them and they will be cached. There are countless examples on the web, I trust you can use Google ;-)

    However you can use the onSubmit handler in the browser to execute some javascript before your form submits. Remember though: the user can turn it off, or otherwise muck with it as they see fit so don't rely on it to do any validation or set some needed parameter in your server side code

    If you're trying to do something with the server, then have the Javascript do something and then have the server continue .... well you can't do that, and I'm not really sure why you would want to. Javascript can set cookies itself, google for that one as well.

    Also as a general rule you should use CGI; which will handle cookies for you easily and safely.

Re: Execute javascript before setting cookie headers
by ambrus (Abbot) on Sep 28, 2005 at 21:54 UTC

    You could try making the javascript redirect to another page after it's done its work, and that other page can set the cookies.