SPUG: Ajax question - js args

Gary Hawkins ghawk at eskimo.com
Tue Jun 20 21:35:34 PDT 2006

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"


  (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"


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

var input   = (scripts[scripts.length - 1]).src.replace(/^[^?]+\??/,''); //


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


       if (http_request.readyState == 4) {  // Replace the image with something

              document.getElementById('foo').innerHTML =





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





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




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


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:
?s=dvd&v=glance&n=130>  .where it says something like
4922?%5Fencoding=UTF8> 56 used & new (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"


...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+ at ARGV>  equivalent of @ARGV. $ENV{'QUERY_STRING'}  <edited by gh>




















-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/spug-list/attachments/20060620/7d8bd1fe/attachment-0001.html 

More information about the spug-list mailing list