ChatGPT解决这个技术问题 Extra ChatGPT

How to store arbitrary data for some HTML tags

I'm making a page which has some interaction provided by javascript. Just as an example: links which send an AJAX request to get the content of articles and then display that data in a div. Obviously in this example, I need each link to store an extra bit of information: the id of the article. The way I've been handling it in case was to put that information in the href link this:

<a class="article" href="#5">

I then use jQuery to find the a.article elements and attach the appropriate event handler. (don't get too hung up on the usability or semantics here, it's just an example)

Anyway, this method works, but it smells a bit, and isn't extensible at all (what happens if the click function has more than one parameter? what if some of those parameters are optional?)

The immediately obvious answer was to use attributes on the element. I mean, that's what they're for, right? (Kind of).

<a articleid="5" href="link/for/non-js-users.html">

In my recent question I asked if this method was valid, and it turns out that short of defining my own DTD (I don't), then no, it's not valid or reliable. A common response was to put the data into the class attribute (though that might have been because of my poorly-chosen example), but to me, this smells even more. Yes it's technically valid, but it's not a great solution.

Another method I'd used in the past was to actually generate some JS and insert it into the page in a <script> tag, creating a struct which would associate with the object.

var myData = {
    link0 : {
        articleId : 5,
        target : '#showMessage'
        // etc...
    },
    link1 : {
        articleId : 13
    }
};

<a href="..." id="link0">

But this can be a real pain in butt to maintain and is generally just very messy.

So, to get to the question, how do you store arbitrary pieces of information for HTML tags?


H
Honza Zidek

Which version of HTML are you using?

In HTML 5, it is totally valid to have custom attributes prefixed with data-, e.g.

<div data-internalid="1337"></div>

In XHTML, this is not really valid. If you are in XHTML 1.1 mode, the browser will probably complain about it, but in 1.0 mode, most browsers will just silently ignore it.

If I were you, I would follow the script based approach. You could make it automatically generated on server side so that it's not a pain in the back to maintain.


@Tchalvak: True, but this bit will work on most browsers, none the less.
Others have claimed that there's no reason to wait on support, since the only problem this causes is failure to validate, and it doesn't break IE. See T.J. Crowler's answer here: stackoverflow.com/questions/1923278/…
If you do use data-xxx attributes and you want to retrieve them, you can simply use the "domElement.getAttribute('data-whatever')" without any third-party framework.
Reminder: In order to retrieve the data, 1337, via jquery, be sure to remove 'data' from the variable name. For eg, use: $(this).data('internalid'); Instead of: $(this).data('data-internalid');
P
Prestaul

If you are using jQuery already then you should leverage the "data" method which is the recommended method for storing arbitrary data on a dom element with jQuery.

To store something:

$('#myElId').data('nameYourData', { foo: 'bar' });

To retrieve data:

var myData = $('#myElId').data('nameYourData');

That is all that there is to it but take a look at the jQuery documentation for more info/examples.


L
Luca Matteis

Just another way, I personally wouldn't use this but it works (assure your JSON is valid because eval() is dangerous).

<a class="article" href="link/for/non-js-users.html">
    <span style="display: none;">{"id": 1, "title":"Something"}</span>
    Text of Link
</a>

// javascript
var article = document.getElementsByClassName("article")[0];
var data = eval(article.childNodes[0].innerHTML);

+1 for lateral thinking. I agree that I probably wouldn't want to use this method, but it's a vaguely viable option!
@nickf You can get rid of eval using JSON.parse instead jsfiddle.net/EAXmY
E
Eran Galperin

Arbitrary attributes are not valid, but are perfectly reliable in modern browsers. If you are setting the properties via javascript, than you don't have to worry about validation as well.

An alternative is to set attributes in javascript. jQuery has a nice utility method just for that purpose, or you can roll your own.


Why not use data- attributes instead?
E
ErickBest

A hack that's going to work with pretty much every possible browser is to use open classes like this: <a class='data\_articleid\_5' href="link/for/non-js-users.html>;

This is not all that elegant to the purists, but it's universally supported, standard-compliant, and very easy to manipulate. It really seems like the best possible method. If you serialize, modify, copy your tags, or do pretty much anything else, data will stay attached, copied etc.

The only problem is that you cannot store non-serializable objects that way, and there might be limits if you put something really huge there.

A second way is to use fake attributes like: <a articleid='5' href="link/for/non-js-users.html">

This is more elegant, but breaks standard, and I'm not 100% sure about support. Many browsers support it fully, I think IE6 supports JS access for it but not CSS selectors (which doesn't really matter here), maybe some browsers will be completely confused, you need to check it.

Doing funny things like serializing and deserializing would be even more dangerous.

Using ids to pure JS hash mostly works, except when you try to copy your tags. If you have tag <a href="..." id="link0">, copy it via standard JS methods, and then try to modify data attached to just one copy, the other copy will be modified.

It's not a problem if you don't copy tags, or use read only data. If you copy tags and they're modified you'll need to handle that manually.


storing valse on class is awesome
K
Kirby

Using jquery,

to store: $('#element_id').data('extra_tag', 'extra_info');

to retrieve: $('#element_id').data('extra_tag');


t
tvanfosson

I know that you're currently using jQuery, but what if you defined the onclick handler inline. Then you could do:

 <a href='/link/for/non-js-users.htm' onclick='loadContent(5);return false;'>
     Article 5</a>

p
phylae

You could use hidden input tags. I get no validation errors at w3.org with this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <meta content="text/html;charset=UTF-8" http-equiv="content-type" />
    <title>Hello</title>
  </head>
  <body>
    <div>
      <a class="article" href="link/for/non-js-users.html">
        <input style="display: none" name="articleid" type="hidden" value="5" />
      </a>
    </div>
  </body>
</html>

With jQuery you'd get the article ID with something like (not tested):

$('.article input[name=articleid]').val();

But I'd recommend HTML5 if that is an option.


I actually don't think style="display: none" is needed for hidden input fields.
Good approach, totally valid in all HTML versions. I stronly discourage coding with the assumption that all your users will have a totally HTML5-complaint browser. And display:none is not needed for hidden fields.
Very nice. This is the best solution I have found for XHTML where data-attributes aren't a valid option. IMO it doesn't abuse tags/attributes in unintended ways, it has almost no "smell" at all. And as the others said: display:none isn't really needed.
佚名

Why not make use of the meaningful data already there, instead of adding arbitrary data?

i.e. use <a href="/articles/5/page-title" class="article-link">, and then you can programmatically get all article links on the page (via the classname) and the article ID (matching the regex /articles\/(\d+)/ against this.href).


Problem with this is that it's also not really extensible
R
Robin Smidsrød

As a jQuery user I would use the Metadata plugin. The HTML looks clean, it validates, and you can embed anything that can be described using JSON notation.


B
BMiner

This is good advice. Thanks to @Prestaul

If you are using jQuery already then you should leverage the "data" method which is the recommended method for storing arbitrary data on a dom element with jQuery.

Very true, but what if you want to store arbitrary data in plain-old HTML? Here's yet another alternative...

<input type="hidden" name="whatever" value="foobar"/>

Put your data in the name and value attributes of a hidden input element. This might be useful if the server is generating HTML (i.e. a PHP script or whatever), and your JavaScript code is going to use this information later.

Admittedly, not the cleanest, but it's an alternative. It's compatible with all browsers and is valid XHTML. You should NOT use custom attributes, nor should you really use attributes with the 'data-' prefix, as it might not work on all browsers. And, in addition, your document will not pass W3C validation.


not sure exactly, but some browsers might complain if you use custom attributes with a "strict" doctype. Either way, it's not valid XHTML.
d
demonkoryu

I advocate use of the "rel" attribute. The XHTML validates, the attribute itself is rarely used, and the data is efficiently retrieved.


cant do that id break the nofollow attribute on the links
S
Shanison

So there should be four choices to do so:

Put the data in the id attribute. Put the data in the arbitrary attribute Put the data in class attribute Put your data in another tag

http://www.shanison.com/?p=321


@h4ck3rm1k3 ...not in the id attribute because it must be unique whithing the document, and it should be repeated if necessary in a sidebar, or something... It's an old question, but it's still valid
Y
Yeti

You could use the data- prefix of your own made attribute of a random element (<span data-randomname="Data goes here..."></span>), but this is only valid in HTML5. Thus browsers may complain about validity.

You could also use a <span style="display: none;">Data goes here...</span> tag. But this way you can not use the attribute functions, and if css and js is turned off, this is not really a neat solution either.

But what I personally prefer is the following:

<input type="hidden" title="Your key..." value="Your value..." />

The input will in all cases be hidden, the attributes are completely valid, and it will not get sent if it is within a <form> tag, since it has not got any name, right? Above all, the attributes are really easy to remember and the code looks nice and easy to understand. You could even put an ID-attribute in it, so you can easily access it with JavaScript as well, and access the key-value pair with input.title; input.value.


I am sure you didn't work with html tables and selection quite enough. You'll be using the data- attribute more often to save some work.
I do use the 'data-' attribute a lot actually. But it depends on what your requirements are. For example, with the you can have any key, whereas for 'data-' this is limited to preferably a lowercase string without any non-alphanumeric character.
K
Kris

As long as you're actual work is done serverside, why would you need custom information in the html tags in the output anyway? all you need to know back on the server is an index into whatever kind of list of structures with your custom info. I think you're looking to store the information in the wrong place.

I will recognize, however unfortunate, that in lots of cases the right solution isn't the right solution. In which case I would strongly suggest generating some javascript to hold the extra information.

Many years later:

This question was posted roughly three years before data-... attributes became a valid option with the advent of html 5 so the truth has shifted and the original answer I gave is no longer relevant. Now I'd suggest to use data attributes instead.

<a data-articleId="5" href="link/for/non-js-users.html">

<script>
    let anchors = document.getElementsByTagName('a');
    for (let anchor of anchors) {
        let articleId = anchor.dataset.articleId;
    }
</script>

how are you supposed to pass data to javascript then?
C
ChrisW

One possibility might be:

Create a new div to hold all the extended/arbitrary data

Do something to ensure that this div is invisible (e.g. CSS plus a class attribute of the div)

Put the extended/arbitrary data within [X]HTML tags (e.g. as text within cells of a table, or anything else you might like) within this invisible div


R
Raphaël

Another approach can be to store a key:value pair as a simple class using the following syntax :

<div id="my_div" class="foo:'bar'">...</div>

This is valid and can easily be retrieved with jQuery selectors or a custom made function.


A
Akash Ghosh

In html, we can store custom attributes with the prefix 'data-' before the attribute name like

<p data-animal='dog'>This animal is a dog.</p>. Check documentation

We can use this property to dynamically set and get attributes using jQuery like: If we have a p tag like

<p id='animal'>This animal is a dog.</p>

Then to create an attribute called 'breed' for the above tag, we can write:

$('#animal').attr('data-breed', 'pug');

To retrieve the data anytime, we can write:

var breedtype = $('#animal').data('breed');


J
Jerry

At my previous employer, we used custom HTML tags all the time to hold info about the form elements. The catch: We knew that the user was forced to use IE.

It didn't work well for FireFox at the time. I don't know if FireFox has changed this or not, but be aware that adding your own attributes to HTML elements may or may-not be supported by your reader's browser.

If you can control which browser your reader is using (i.e. an internal web applet for a corporation), then by all means, try it. What can it hurt, right?


j
jason

This is how I do you ajax pages... its a pretty easy method...

    function ajax_urls() {
       var objApps= ['ads','user'];
        $("a.ajx").each(function(){
               var url = $(this).attr('href');
               for ( var i=0;i< objApps.length;i++ ) {
                   if (url.indexOf("/"+objApps[i]+"/")>-1) {
                      $(this).attr("href",url.replace("/"+objApps[i]+"/","/"+objApps[i]+"/#p="));
                   }
               }
           });
}

How this works is it basically looks at all URLs that have the class 'ajx' and it replaces a keyword and adds the # sign... so if js is turned off then the urls would act as they normally do... all "apps" (each section of the site) has its own keyword... so all i need to do is add to the js array above to add more pages...

So for example my current settings are set to:

 var objApps= ['ads','user'];

So if i have a url such as:

www.domain.com/ads/3923/bla/dada/bla

the js script would replace the /ads/ part so my URL would end up being

www.domain.com/ads/#p=3923/bla/dada/bla

Then I use jquery bbq plugin to load the page accordingly...

http://benalman.com/projects/jquery-bbq-plugin/


b
biofractal

I have found the metadata plugin to be an excellent solution to the problem of storing arbitrary data with the html tag in a way that makes it easy to retrieve and use with jQuery.

Important: The actual file you include is is only 5 kb and not 37 kb (which is the size of the complete download package)

Here is an example of it being used to store values I use when generating a google analytics tracking event (note: data.label and data.value happen to be optional params)

$(function () {
    $.each($(".ga-event"), function (index, value) {
        $(value).click(function () {
            var data = $(value).metadata();
            if (data.label && data.value) {
                _gaq.push(['_trackEvent', data.category, data.action, data.label, data.value]);
            } else if (data.label) {
                _gaq.push(['_trackEvent', data.category, data.action, data.label]);
            } else {
                _gaq.push(['_trackEvent', data.category, data.action]);
            }
        });
    });
});

<input class="ga-event {category:'button', action:'click', label:'test', value:99}" type="button" value="Test"/>