0

I need business data inside my HTML pages. This data is not directly related to the DOM, but is used to handle javascript (and/or jquery) operations on the DOM. For example I shoud know that an object with id=100 holds objects with ids 1001, 1002 and 1003.

object (id=1000) = {1001,1002,1003}

I've been asking around but I hear different ideas.

Some solutions so far now

A datalist.

<dl id="1000">
  <dt>1001</dt>
  <dt>1002</dt>
  <dt>1003</dt>
</dl>

Classes from tags

<span id="1000" class="o_1001 o_1002 o_1003">
      (or any other tag ?)
<dl id="1000" class="o_1001 o_1002 o_1003">

UPDATED : Not in HTML at all? Since it's about data that is needed for javascript I might just as well put it directly in javascript?

<script>
   var object = { id : 1000, value : [1000,1001,1002]; }
   storeGlobal(object);
</script>

And storeGlobal would be a globar var in a js-script, that now also has the data.

Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Peter
  • 47,963
  • 46
  • 132
  • 181

2 Answers2

1

If the data is in some way related to some element(s) in the DOM, you could use jQuery.data(). It's a very nice and clean way of storing data in a page.

If it's not related to any DOM element, it's probably easiest to handle it completely in JS as an array/object.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

If it's just data you will use in your javascript, write it as javascript and be done with it. Don't shove it into the DOM for no particular reason. You can either just have it as javascript literals (possibly generated by something like JSON.stringify on the server) or as a distinct JSON document your javascript retrieves via an ajax request.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • I have to create only a div (eg
    ) and the javascript is in a separate file. But indeed I can use my third option and create
    – Peter Aug 19 '11 at 08:27