SPUG: Ajax question - js args

Jay Scherrer jay at scherrer.com
Wed Jun 21 15:07:58 PDT 2006


Gary Hawkins wrote:
>
> Got it.
>
> Ajax goes pretty well with Perl, but javascript doesn’t make it easy 
> to pass arguments around, and that’s essential sometimes. So I think 
> having a chunk of code in the toolbox for parsing input sent to a 
> remote .js file (think caching efficiency) from a webpage that uses 
> Ajax is worth a bit of time here, so I’ll share it. Thanks to those 
> who replied.
>
> This solution basically started here: 
> http://feather.elektrum.org/book/src.html, a better than average 
> writeup on it, and coincidentally, traction began when I scanned 
> cached js files on my drive that contained ‘parseQuery’ from that 
> page. I happened to run into a file called WhiskerBiscuit[1].js, 
> apparently a cookie-cutter utility for parsing, from that same source, 
> with this attribution:
>
> /*
>
> Code from "Developing Featherweight Web Services with JavaScript"
>
> http://feather.elektrum.org/
>
> (c)An Elektrum Press, retain this notice
>
> License: http://feather.elektrum.org/appendix/licenses.html
>
> */
>
> Boiling their rather heavy file down and extracting just some 
> necessary elements from it, the two lines in bold below do the parsing 
> of the input string ‘a=B000ERVJKO’ sent by the following line:
>
> <script language="javascript" 
> src="http://www.eskimo.com/~ghawk/ajax/test.js?a=B000ERVJKO"></script>
>
> // Parse inputs arguments
>
> // js something-like-ish-ness of Perl's $ENV{'QUERY_STRING'}, only not 
> really.
>
> // Two steps for easy reading. Basically the script looking in the mirror:
>
> *var scripts = document.getElementsByTagName('script');* // from 
> <script ...></script>
>
> *var input = (scripts[scripts.length - 
> 1]).src.replace(/^[^?]+\??/,'');* // trim
>
> // Script that this js (Ajax) will run is test.cgi.
>
> // Here, relaying the input on to the Perl script that will be doing the
>
> // heavy lifting even well after the original page is done loading
>
> // (except for the Ajax portion patiently waiting).
>
> var PerlScript = "http://www.eskimo.com/~ghawk/cgi-bin/test.cgi?" + input;
>
> // Ajax, here to end
>
> if (window.XMLHttpRequest) { // FireFox et. al.
>
> http_request = new XMLHttpRequest();
>
> }
>
> else if (window.ActiveXObject) { // IE
>
> http_request = new ActiveXObject("Microsoft.XMLHTTP");
>
> }
>
> var obj = document.getElementById('foo');
>
> http_request.open("GET", PerlScript, true);
>
> http_request.onreadystatechange = function() {
>
> if (http_request.readyState == 1) { // Do something while waiting
>
> document.getElementById('foo').innerHTML = '<img 
> src=http://www.eskimo.com/~ghawk/ajax/loading6.gif>';
>
> }
>
> if (http_request.readyState == 4) { // Replace the image with 
> something meaningful
>
> document.getElementById('foo').innerHTML = http_request.responseText;
>
> }
>
> }
>
> http_request.send(null);
>
> Now I need to know how to set a version so that the client will only 
> spend time grabbing the js code if it is new.
>
> *Gary Hawkins*
>
> Quality Assurance Engineer
>
> * *
>
> W H I T E P A G E S .C O M | I N C*
> *
> p: 206.973.8215 | f: 206.621.1375
>
> ghawkins at whitepages.com
>
> http://www.whitepagesinc.com/
>
> PS Need to s/@ARGV/$ENV{'QUERY_STRING'}/ in my earlier message.
>
> ------------------------------------------------------------------------
>
> *From:* spug-list-bounces+ghawk=eskimo.com at pm.org 
> [mailto:spug-list-bounces+ghawk=eskimo.com at pm.org] *On Behalf Of *Gary 
> Hawkins
> *Sent:* Tuesday, June 20, 2006 12:58 AM
> *To:* spug-list at pm.org
> *Subject:* SPUG: Ajax question - js args
>
> Hiya,
>
> I’m working with Ajax and have a real basic question. Meanwhile I’m 
> going to show what I’m doing in this Ajax example, because surely 
> there will be /(people)+/ that will find it intriguing or useful.
>
> My question is simply: How do I assign input arguments that are passed 
> to a .js file to variables in the js code? That’s new to me, I think 
> I’ve read ~ 42 million web pages looking for that simple building 
> block, and no joy.
>
> Here’s the example: http://www.eskimo.com/~ghawk/ajax/test.htm 
> <http://www.eskimo.com/%7Eghawk/ajax/test.htm>
>
> Once on the page, you’ll see a little “rotating” animated gif while 
> the external page/information is being read. The Perl script triggered 
> by the js code is looking for the number of sellers on this page: 
> http://www.amazon.com/exec/obidos/ASIN/B000ERVJM2/ 
> <http://www.amazon.com/gp/product/B000ERVJM2/ref=pd_ts_d_31/102-0990943-3824922?s=dvd&v=glance&n=130> 
> …where it says something like *56 used & new* 
> <http://www.amazon.com/gp/offer-listing/B000ERVJM2/ref=dp_olp_2/102-0990943-3824922?%5Fencoding=UTF8> 
> (the number changes of course).
>
> Once Perl has retrieved that page and extracted the number, it is 
> returned to the javascript (the js file that was pulled in by the htm 
> page), and Ajax sees the package has arrived, updates the div (id is 
> ‘display’) and places the digits there, replacing the image.
>
> So, you might notice that in the javascript, I am sending an arg:
>
> <script language="javascript" 
> src="http://www.eskimo.com/~ghawk/ajax/test.js?a=B000ERVJKO"></script>
>
> ...and that ASIN (Amazon Standard Identification Number) is different 
> than the one hardcoded (or medium coded) into the .cgi file. Now all I 
> have to do is persuade js to take in that arg so it can be handed over 
> to the Perl script.
>
> In short, I need the js equivalent of @ARGV 
> <http://www.google.com/search?as_q=&num=100&hl=en&btnG=Google+Search&as_epq=js+equivalent+of+@ARGV>. 
> $ENV{‘QUERY_STRING’} <edited by gh>
>
> Thanks,
>
> Gary
>
> ------------------------------------------------------------------------
>
> _____________________________________________________________
> Seattle Perl Users Group Mailing List  
>      POST TO: spug-list at pm.org
> SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
>     MEETINGS: 3rd Tuesdays
>     WEB PAGE: http://seattleperl.org/
Gary,
I may be a little simple minded, but if you are trying to keep track of 
the number of sellers of a certain item, my suggestion would be to use 
your .js script with a function that returns the param's of your perl 
script connecting to a persistent data base. If no information is 
available the default is set to zero and or hidden. This way Perl::CGI 
can easily keep track of all your connections.

I am using this technology with my income tax preperation program and 
works rather simple, rather well.
HTML, JavaScript, Perl::CGI, MySQL.

Jay Scherrer


More information about the spug-list mailing list