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

Here's a very basic newbie question for you:

So far in experience I've ran CGI scripts from links and buttons and from placing absolute URLS directly into the Address bar on the browser. I'd like to be able to call a CGI script and have it return a value without redirecting the browser. This is pretty object oriented in theory:
<html> <body> <div ID=changeDIV>The answer will be here</div> <input type=text value="Put a number here" name=input1><br> <input type=button value="x2" onclick='changeDIV.innerHTML=somePerlCGI +(document.input1.value, 2);'> <input type=button value="x3" onclick='changeDIV.innerHTML=somePerlCGI +(document.input1.value, 3);'> </body> </html>
These calculations aren't my goal, obviously, or else I would do it with JavaScript. I'd like to be able to load new content into a page without reloading the entire page. Can this be done with CGI?

Thanks,
Petras

Replies are listed 'Best First'.
Re: Running CGI Scripts without redirecting the browser
by dws (Chancellor) on Feb 04, 2003 at 09:19 UTC
    I'd like to be able to call a CGI script and have it return a value without redirecting the browser. ... Can this be done with CGI?

    Yes, using frames and JavaScript. The <form> tag has a "target" attribute. Using it, you can direct the CGI's output to a hidden frame. If that output happens to be JavaScript, it can side-effect the document's object mode.

    This is basically the path exploited by "pushlet" technology.

Re: Running CGI Scripts without redirecting the browser
by cfreak (Chaplain) on Feb 04, 2003 at 14:42 UTC

    As others have mentioned there's not really a way to do exactly what you are trying to accomplish, however you can fake it by outputting your CGI to hidden frames and mixing in Javascript.

    That said, its usually more trouble than its worth IMHO. When you fake it then you have a whole new set of problems deciding what to do with people who haven't enabled Javascript or people in say, lynx, that aren't going to have frames at all, not to mention all the inconsistancies in Javascript across browsers.

    I've been building web apps for a long time and I've discovered that customers and designers and other programmers are much much happier when you follow the KISS method. Keeping things simple benefits everyone and makes things easier to maintain down the line.

    There are three reasons I've found that programmers do what you do, the first is somewhat valid. They feel that by loading only portions saves on load time. This is only true if your entire page is covered in text that doesn't change with the CGI script. The static images get cached anyway so a full reload of the page isn't going to help. If the output of your CGI provides new images, well you still have to load them either way.

    The second reason also has validity, alot of people want to do things this way to keep a look consistant without coding alot of HTML into the CGI script. This can be accomplished much easier by using a template system. Yes you will still be reloading static elements of the page but see point one above. My templating system of choice is HTML::Template

    And finally the most common is the gee-whiz factor, trying to make a web-app behave like a local app. I must say that in almost every case that I've used something like this its been confusing to use and/or broken because of browser inconsistancies, pop up blockers, not using IE, etc ... So yeah it might look cool on your browser with your settings but it might be a nightmare for your users.

    So i would suggest that you evaluate why you want this functionality in the first place, because the simpler you can make it now, the less headache it will be for everyone in the future.

    Hope that helps
    Chris

    Lobster Aliens Are attacking the world!
Re: Running CGI Scripts without redirecting the browser
by hardburn (Abbot) on Feb 04, 2003 at 14:51 UTC

    I dug this out of my bookmarks: http://jibbering.com/2002/4/httprequest.html. This describes how to setup a JavaScript to fetch data from the server without reloading the browser window (and without using frames).

    Note: HTTP was designed as a stateless protocol, which has caused no end to annoying hacks like this one. Most of the things people belly-ache about on the web (cookies, JavaScript, etc.) were created to get around HTTP's flaws.

Re: Running CGI Scripts without redirecting the browser
by jacques (Priest) on Feb 04, 2003 at 14:04 UTC
    So far in experience I've ran CGI scripts from links and buttons and from placing absolute URLS directly into the Address bar on the browser. I'd like to be able to call a CGI script and have it return a value without redirecting the browser.

    You can execute CGI scripts at the command line. No browser needed. You can also use CGI.pm's offline mode for debugging. See: http://stein.cshl.org/WWW/software/CGI/#debugging

    But, of course, this doesn't answer your real question:
    I'd like to be able to load new content into a page without reloading the entire page.

    You might be interested in inline frames. But I doubt it.

    Can this be done with CGI?

    CGI scripts are server-side applications. So the short answer is no. But this can be accomplished with JavaScript. What you are interested in is DHTML.