473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

javascript/cgi

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
7 1667
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
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
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

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
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
Just tried it...Ajax does exactly what I want.

Dec 16 '05 #7
Great!

Dec 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.