[sf-perl] OffTopic: Any JavaScript programmers here?

David Scott ds94103 at earthlink.net
Sat Jul 5 07:45:41 PDT 2008

I had a hard time understanding what Vicki was trying to do, but seeing 
her code makes it clear - basically, the list is encoded in HTML, not 
something that comes to you from the back end as a data structure, in an 
Ajax (XHR) call for example.

So the innerHTML thing makes a lot of sense in this case (I think).

But it really is the Hard Way to Do Things in Javascript.  You have to 
back off and ask yourself, why do you need a sorted list in the first 
place?  Aren't the list items coming to you from some source, like a 
database query?  If there is only one list on the page, what's the 
matter with sending a parameter to the back end indicating how you want 
it to sort the data?  If there are more than one, why can't you do an 
XHR call to bring in the data you need, where you need it?  It really 
isn't that hard - the time you spent banging your head against the wall 
on this problem would have been better spent learning how to do that.

Of course if you're scraping someone else's HTML then you don't have a 
lot of choice, and innerHTML could be your friend.  Then again, I'm not 
sure why you're scraping HTML code in Javascript.  I can't quite see the 
use case.

Also, of course JS has hashes.  They are called a variety of things - 
'associative arrays', 'object literals', 'objects', etc. depending on 
where you look.  In fact, Javascript objects are little more than hashes 
(even moreso than in Perl).  You use objects (hashes) all the time in JS 
(eg, the 'window' object is a hash).  If you're actually scraping 
multiple lists, you can store all the lists in an object, for example, 
keyed by the element ID.  Or whatever.

A very good JS reference is David Flanagan's 'Definitive Guide', 5th 
edition.  Douglas Crockford just came out with a good book too.


Vicki Brown wrote:
> At 20:31 -0700 07/04/2008, Kevin Frost wrote:
>> Noooo! Don't poison the novices with innerHTML!  That's like telling
>> people not to 'use strict' in Perl.
> Being a Perl programmer, I can totally understand your trepidation.
> However... speaking as a frustrated JS noobie... the innerHTML worked.
> :-)(-:
> Less frightening variations would be gratefully accepted.
> Also, if anyone has any hints about how to handle more than one sortable
> list on a page, I'd be grateful. :)  The sort function is handed an ID to
> the list to sort.
> In Perl I'd use a hash
>    origList[$id] = #save the list data;
> and then I'd just go looking for origList[$id] when I was asked to revert
> theList[$id]
> Surprisingly, I don't see hashes (or 'associative arrays') in JavaScript.
> Although something described as an 'associative array' is present, it
> doesn't seem to be quite the same thing.
> Current code is here:
>    http://cfcl.com/twikipub/Learn/Tut/TWikiTutListSort/list_sort.js
> Example page
>    http://cfcl.com/twiki/bin/view/Learn/Tut/TWikiTutListSort
> And many thanks for helping me in this educational exercise... if any of
> you have a TWiki you're running and have questions about how to do
> something there, just ask!!!

More information about the SanFrancisco-pm mailing list