zwol: stylized sketch of a face in profile (Default)
[personal profile] zwol
exactly which circle of browser compatibility hell will I be doomed to if I make up my own DIV attributes in a (machine generated) HTML page for the benefit of the Javascript program inside? I mean, like, <div id="foo" xprev="bar" xnext="quux"> instead of having on-the-side associative arrays. (Does Javascript even have associative arrays? It must, but all the documentation I can find online fails to inform, and fails to inspire confidence in what little information it does provide.)

javascript assoc arrays

Date: 2007-04-24 08:35 am (UTC)
From: [identity profile] tkil.livejournal.com
It makes me feel a little dirty every time I use them, but apparently the proper JS technique is to get a new Object and just use attributes on that object to give you key/value pairs. Using square brackets makes it a bit more palatable, but still...

Date: 2007-04-24 09:04 am (UTC)
From: [identity profile] xorphus.livejournal.com
Adding attributes will invalidate the XML and throw the browser into quirks mode, so your styling will probably look different than you expect, but it's not going to blow anything up. Afaik.

Date: 2007-04-24 09:47 pm (UTC)
From: [identity profile] queenpam.livejournal.com
var assocarray = new Array();
assocarray['one'] = 1;
assocarray['two'] = 2;
alert(assocarray['two']);

Technically it's not working quite like an associative array, but it does the same thing. You could just say = new Object(), not that there's a whole lot of difference there in javascript. I think you can also do

var assocarray = {'one':1, 'two':2};
alert(assocarray['two']);

Date: 2007-04-24 09:52 pm (UTC)
From: [identity profile] queenpam.livejournal.com
heh, I just tried it and var assocarray = new String(); will work too :)
(that would probably make that first person feel dirty too, but the way objects work is one of my favorite things about javascript)

Date: 2007-04-25 03:17 am (UTC)
From: [identity profile] zwol.livejournal.com
I found the {'one':1, 'two':2} syntax this morning. It's great because it's exactly the same syntax that Python's built-in dictionary serializer uses. No tedious generator to write!

Date: 2007-04-25 03:26 am (UTC)
Page generated Feb. 12th, 2026 03:59 pm
Powered by Dreamwidth Studios