I need to access general information from my site using javascript. So far, I have the following options:
Using an html element :
<input type="hidden" value="MyValue"/>Using a custom attribute in an existing html element :
<div id="HtmlElement" myattr="MyValue"></div>and then access it usingdocument.getElementById("HtmlElement").getAttribute("myattr")Using a data attribute in an existing html element:
<div id="HtmlElement" data-myattr="MyValue"></div>and then access it usingjQuery("#HtmlElement").data("myattr")
I was wondering what are the benefits of using either option.
I'm not a fan of using hidden inputs because I don't like the idea of having a loose html element that contains information. But since I need it to display general information, not information related to an existing html element in the page, it doesn't seem so bad.
On the other side, I'm not a fan of abusing the use of an external library but in my case I'm allready loading jQuery in my site, so it's not as if i was loading an entire library just for this.
And finally, even dough performance is allways an issue, in my case it's not gonna make much difference if it's the fastest solution.