[whatwg] Attribute for holding private data for scripting

Kevin Marks kevinmarks at gmail.com
Wed Apr 11 12:35:15 PDT 2007


On 4/11/07, Jon Barnett <jonbarnett at gmail.com> wrote:
>
>
> On 4/11/07, Kevin Marks <kevinmarks at gmail.com> wrote:
> > On 4/11/07, Jon Barnett <jonbarnett at gmail.com> wrote:
> > > > If you want structured data in this attribute, why not just use JSON?
> > >
> > > That's an idea that crossed my mind as well.  I dismissed it for a few
> > > reasons:
> > > - authors would have to entitize quotes and ampersands in their
> attributes,
> > > which they're not used to doing with JSON normally.
> > > - evaluating it would mean:
> > > var obj = eval( myelement.getAttribute("_myjson");
> >
> > How about defining an attribute that is the name of the js variable
> > for use with that element? Then you can define the variable in a
> > <script> tag, and use pure JSON cleanly.
>
> I don't understand what you mean there.  It was said that we don't need to
> add something new to the DOM.  If I understand, you're suggesting a single
> attribute hypothetically called "params" spec'ed to be a JSON format:
> <div params="{foo: 'bar', bish: &quot;bash&quot;}"></div>
> with the DOM attribute named params that parses that attribute as JSON into
> an object so that something like this happens in JavaScript:
> ...
> mydiv.params.foo == 'bar'; // it is!
>
> While that would be nice, it's not something browsers currently do, and the
> goal is to spec something that today's browsers already handle and HTML5
> validators will be happy with.  Granted, you can use eval() in Javascript to
> get what you want in todays browsers, but is it best to actually spec it
> that way?

No, what I'm suggesting is that you have, say, a 'localdata' attribute
that names the associated variable:
<script>myparams={"foo":"bar","bish":"bash"};</script>
<div class="mydiv" localdata="myparams">

mydiv.localdata.foo =="bar"; // it is

I think making this work in current browsers would be doable by having
a script that creates the DOM elements by looking for the 'localdata'
parameters.



More information about the whatwg mailing list