javascript - Is it bad to add JSON on HTML data attribute? -
since html data
attribute allows adding custom data, wonder if idea include set of json
list data
attribute? then, corresponding json
can accessed javascript
events getattribute("data-x")
.
in fact, question that: standard, efficient, , reasonable add large set of data html
attribute?
for example
<div data-x="a large set of json data" id="x">
or large set of json data must stored within <script>
tag, , html
attribute not right place large set of data, data
attribute.
instead of storing in data attribute use identifier access data.
so example :
var mybigjsonobj = { data1 : { //lots of data}, data2 : { //lots of data} };
and had html :
<div data-dataid="data1" id="x">
you can use jquery data :
var dataid = $('#x').attr('data-dataid'); var mydata = mybigjsonobj[dataid];
this best approach imho.
Comments
Post a Comment