By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,401 Members | 793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,401 IT Pros & Developers. It's quick & easy.

javascript/cgi

P: n/a
I know this subject has been well discussed, but I haven't found
exactly what I'm looking for in the archives, or if it's possible.

I need to call a cgi (actually, mod_perl) script, but not on every page
hit - preferably just on the first. I'm dealing with a static site and
what I'd like to do is use javascript to read a cookie, and if the
cookie is found, I'll use the value from the cookie to display some
html.

However, if the cookie isn't found, then I need to call a perl script
that will do some processing, set a cookie, but somehow I need to get
the value back - the same value that is used to set the cookie -
without reloading the page. I know I can use javascript
src="/path/to/myscript" and have the Perl script write javascript
(setting my variable which I can use later), but then I couldn't use
the "if" logic to see if I actually need to call the Perl script as the
client side javascript will be overridden by the "src" attribute...

Example code...
--
// my sub, returns value for cookie name 'country'
var country = GetCookie('country');

if (country == null) {
alert('No cookie found - Call script/Set cookie');
var url = "/cgi-bin/geo.cgi";
// Here's what I need....
// country=output of cgi
// Perl script will set cookie with country,
// but I also need the value of country returned here,
// which the Perl script will output
} else {
alert('Cookie found ' + country);
}

document.write('<p>You live in, '+country);
--
Hope that makes sense....thanks.

Dec 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Can't you test for the cookie using perl at the top of the page? Ya
know, like (I don't know Perl, but this is PHP [close]):

<?php
$country = $_COOKIE["country"];

if (!isset($country) || empty($country)) {
//Your server code ($errormsg = ..., or whatever)
}
?>

I not clear on what you're trying to accomplish, though.

Dec 15 '05 #2

P: n/a
spariam wrote:
I need to call a cgi (actually, mod_perl) script, but not on every page
hit - preferably just on the first. I'm dealing with a static site and
what I'd like to do is use javascript to read a cookie, and if the
cookie is found, I'll use the value from the cookie to display some
html.


This sounds like a job for "just mod_perl".

Have a content handler that checks for the presence of the cookie and
declines to handle the request if its found, otherwise it sets the cookie
and redirects (to somewhere which doesn't demand the cookie - you don't
want to get people with cookies disabled stuck in an infinate loop!).

Another (possibly better) option, that doesn't need JavaScript or Cookies
would be to have the handler check for the cookie, if it finds it, it
declines to handle the request, and the server retrieves the page as
normal. If the cookie isn't there, then it sets the cookie, looks at the
requested URL, opens that file, looks in it for a specially formatted HTML
comment (you could borrow SSI syntax), and replaces it with the text you
want.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Dec 15 '05 #3

P: n/a
This issue is that the site is static...there's no mod_perl, no php, no
SSI on the server where the html files reside. That's why I'd like to
use javascript...however, I do need some dynamic action, which is why
I'd like to call an external mod_perl script. If we ran SSI, I'm pretty
sure a simple "exec" call would work, but as it stands, the mod_perl
script will have to reside on a different machine.

What I'm trying to accomplish is this...we need to display special html
for IPs that map to the US (using Geo::IP on the box that is running
mod_perl). So to limit the load to the mod_perl box, I'd like to use a
cookie check with javascript to see if the user has already visited
once and (hopefully) had a cookie set that contains the country code.
If no cookie is found (via javascript), then we make the remote call to
the external mod_perl script to set the cookie and somehow return the
country code back to the javascript. Actually, if I could get the
country code back in the javascript, I could set the cookie with
javascript too (assuming the client has javascript turned on, of
course).

So think of the call to the mod_perl script get the country call as a
"service" that any static page can call...

Dec 16 '05 #4

P: n/a

spariam wrote:
This issue is that the site is static...there's no mod_perl, no php, no
SSI on the server where the html files reside. That's why I'd like to
use javascript...however, I do need some dynamic action, which is why
I'd like to call an external mod_perl script.
[snip]
If no cookie is found (via javascript), then we make the remote call to
the external mod_perl script to set the cookie and somehow return the
country code back to the javascript. Actually, if I could get the
country code back in the javascript, I could set the cookie with
javascript too (assuming the client has javascript turned on, of
course).

So think of the call to the mod_perl script get the country call as a
"service" that any static page can call...


Oh, ok - I see what you're trying to do. Well, this sounds exactly
like a job for so-called AJAX (using the XMLHttpRequest object). This
would accomplish everything you're wanting to do - are you familiar
with this?

Dec 16 '05 #5

P: n/a
Joel Byrd wrote:
Oh, ok - I see what you're trying to do. Well, this sounds exactly
like a job for so-called AJAX (using the XMLHttpRequest object). This
would accomplish everything you're wanting to do - are you familiar
with this?


Yes, I'm familiar with AJAX, but I've never really looked at it
closely. Guess that'll be on tomorrow's schedule...

Dec 16 '05 #6

P: n/a
Just tried it...Ajax does exactly what I want.

Dec 16 '05 #7

P: n/a
Great!

Dec 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.