javascript/cgi

Discussion in 'Javascript' started by spariam, Dec 15, 2005.

  1. spariam

    spariam Guest

    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.
     
    spariam, Dec 15, 2005
    #1
    1. Advertising

  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
     
    David Dorward, Dec 15, 2005
    #2
    1. Advertising

  3. spariam

    Joel Byrd Guest

    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.
     
    Joel Byrd, Dec 15, 2005
    #3
  4. spariam

    spariam Guest

    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...
     
    spariam, Dec 16, 2005
    #4
  5. spariam

    Joel Byrd Guest

    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?
     
    Joel Byrd, Dec 16, 2005
    #5
  6. spariam

    spariam Guest

    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...
     
    spariam, Dec 16, 2005
    #6
  7. spariam

    spariam Guest

    Just tried it...Ajax does exactly what I want.
     
    spariam, Dec 16, 2005
    #7
  8. spariam

    Joel Byrd Guest

    Great!
     
    Joel Byrd, Dec 16, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jürgen Exner

    Re: CGI Perl "use CGI" statement fail

    Jürgen Exner, Jul 31, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,618
    Jürgen Exner
    Jul 31, 2003
  2. Shailan
    Replies:
    2
    Views:
    1,158
    Shailan
    Dec 15, 2003
  3. John Smith
    Replies:
    0
    Views:
    3,242
    John Smith
    May 15, 2006
  4. LarsenMTL
    Replies:
    4
    Views:
    854
    Eric Walstad
    Nov 4, 2004
  5. praba kar

    Python-cgi or Perl-cgi script doubt

    praba kar, Jul 30, 2005, in forum: Python
    Replies:
    1
    Views:
    810
    Michael Sparks
    Jul 30, 2005
Loading...

Share This Page