javascript/cgi

S

spariam

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

David Dorward

spariam said:
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.
 
J

Joel Byrd

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

spariam

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

Joel Byrd

spariam said:
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?
 
S

spariam

Joel said:
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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top