3

I've got some java script code that implements a kind of slide show. It uses a series of img tags as a table to control its actions. I probably used some code I found some where as a basis. Anyway, the img tags contain a data-img attribute which I can't find in any definition of the img tag. Now I find I need to add more data to the img tags. So my questions are:

1) Is the data-img actually a real attribute or something adhoc?

2) Can I invent yet more attributes?

3) What is the danger of using such attributes?

Mike D
  • 2,753
  • 8
  • 44
  • 77

3 Answers3

2

1) It's valid in HTML 5; it is an invalid attribute (that will, as far as I know, still work in all browsers, it but will break validation) in HTML 4

2) As far as I am aware, you can add arbitrary attributes and query them in Javascript, but chances are those attributes will not survive certain DOM manipulation and other operations where the browser creates markup - it's possible the invalid attributes will simply be dropped

3) Your pages will no longer validate.

Background: Custom attributes - Yay or nay?

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • From the reference you cite it seems that perhaps data-anything is ok in HTML 5. Is that correct? Also see http://html5doctor.com/html5-custom-data-attributes/ – Mike D Nov 20 '10 at 14:42
  • @Mike data-anything is definitely valid and fine in HTML 5, yes. – Pekka Nov 20 '10 at 16:15
0
  1. Depends on implementation.
  2. YES, its often quite useful to do so.
  3. If created in #2, not really, no, they are part of the DOM at that point, i know of no browser that removes attributes.
grmartin
  • 1,399
  • 2
  • 12
  • 23
  • The browser removes attributes that he is not aware of. That means that IE8 and lower have no idea what he is talking about. – jwueller Nov 20 '10 at 14:03
  • @elusive when does the browser remove unknown attributes? Surely not in a normal HTML / Javascript page context? – Pekka Nov 20 '10 at 14:09
  • @Pekka: I ran into problems with custom attributes some time ago and figured that it only happened in IE. I am not sure of the context, though. – jwueller Nov 20 '10 at 15:20
0

They are a valid HTML5 attributes.

If you need to add and get that data only with javascript for client side without communicating with the server side you can implement a store and retrieve mechanism similar to the one used by Mootools.

It doesn't pollute the DOM and store also javascript objects.

yuri
  • 575
  • 1
  • 14
  • 33