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

Hi monks,

I've been doing some work with CGI::AJAX, and I stumbled upon a tutorial about AJAX loading screens using the javascript code below:

<script type="text/javascript"> function init () { $('submit').onclick = function () { sendData(); } } function sendData () { var url = 'process.php'; var pars = Form.serialize('frm'); var myAjax = new Ajax.Request( url, {method: 'get', parameters: pa +rs, onLoading: showLoad, onComplete: showResponse} ); } function showLoad () { $('load').style.display = 'block'; } function showResponse (originalRequest) { var newData = originalRequest.responseText; $('load').style.display = 'none'; $('status').innerHTML = newData; } </script> </head> <body onload="init()"> <div id="load" style="display:none">loading...</div> <div id="status"></div>
This is easily attained using traditional AJAX, however I've not been able to figure out how I might do this using pure CGI::AJAX.

Any thoughts?

meh.

Replies are listed 'Best First'.
Re: CGI::AJAX loading screen?
by bart (Canon) on Mar 10, 2007 at 07:50 UTC
    A tiny comment that might make the Javascript code a bit more understandable to mere humans: in Javascript, '$' is a normal character for variable names. So in this example, $ is the name of a function, most likely defined in the (Javascript) library loaded by your Perl library, and I assume it does something along the lines of document.getElementById. Probably.

    update I've tested it interactively in Firefox (with Firebug), on this site, and $ is a defined function. Weird. I don't know how standard it is, I had never heard of it before, so I'm not absolutely works in other browsers, but it is apparently indeed a shortcut for document.getElementById(id). But, it's a pretty hard "word" to Google for...

    update Found it. It apparears to be defined in the Prototype library, and I think some extension I have loaded must depend on it.

      Its actually a pretty uber function, "prototype function" http://www.google.com/search?q=javascript+dollar+function
Re: CGI::AJAX loading screen?
by erroneousBollock (Curate) on Mar 10, 2007 at 07:03 UTC
    Do you really dislike Javascript to that extent?

    If even a small number of the things you can possibly do with Javascript (in a web browser) were translated to have "server-side" implementations in CGI::Ajax (or GWT and - to some extent - ASP.net), those libraries would become massive, unwieldy beasts.

    Javascript is a nice language for writing the client-side portion of a web application. It is a *small* language that has easily understandable semantics, but is not lacking in core features.

    Javascript has a different kind of object-orientation than Java/C++, but as a perl coder that should not bother you ;)

    It supports excellent (free) libraries including: Dojo, Mochikit, Prototype, YUI.

    Unless/until perl becomes the defacto client-side web-application language (don't hold your breath), you'd me much better off learning Javascript.

    I thoroughly recommend a combination of:
    • client written as HTML with "unobtrusive" Javascript, using the Dojo toolkit.
    • server implemented as Apache with mod_perl/FastCGI.
    • functionality written as perl classes dispatched from SOAP::Lite or JSONRPC.
    -David.

      It's not so much that I dislike it, I just wanted to stay consistent. There's probably going to be a lot more functionality I'll need than CGI::AJAX can provide, so I'm sure I'll end up writing some javascript anyway.

      I'll look in to what you've recommended, Lord knows I could use help in learning it :-)

      Thanks for the advice!

      meh.