views:

36

answers:

2

Hi, this question isn't related to jQuery itself but I found a plugin named Metadata found there and one of the example uses custom tag attribute: <li data="{some:'random', json: 'data'}">...</li>.

Q: Is that cross-browser? Will this fail when validating markup?

Thanks.

+3  A: 

The browser won't care, since very, very few browsers actually validate the HTML. It will fail if you try to treat it as XHTML though, since it isn't valid XHTML.

Ignacio Vazquez-Abrams
Does that mean I have to remove my `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">` or I can still use it but trying to validate my markup will be useless?
Cybrix
To be completely compliant you *should* remove the doctype declaration. Most browsers won't care though.
Ignacio Vazquez-Abrams
And, am I wrong or it won't affect S.E.O. at all?
Cybrix
I can't see it affecting SEO in any way.
Ignacio Vazquez-Abrams
One last question before I accept your answer. It's not part of the main question though: I think I will use class attribute to store data. Is there anything wrong doing so? (I will still accept your answer if I dont get any response for this one)
Cybrix
It is feasible to use the `class` attribute to store state, but I would not recommend using it for arbitrary data.
Ignacio Vazquez-Abrams
@Cybrix - You could use the rel and rev attributes to store data. They're valid, and it's a common approach used by many jQuery plugins.
mellowsoon
What you *could* do though is use a separate namespace for your custom attributes. Providing your own DTD for them will not cause validation to fail.
Ignacio Vazquez-Abrams
@mellowsoon Thank you. It's right and I totally forgot about those. @Ignacio is there a link with an example or could you provite an example?
Cybrix
`<html xmlns:somedata="http://example.com/somedata.dtd"> ... <li somedata:data="{some:'random', json: 'data'}">`
Ignacio Vazquez-Abrams
I like that technique. It looks clean.
Cybrix
+2  A: 

The browser wont care. Most (if not all browsers) just ignore illegal attributes. If you try to validate it, it WILL fail however. What you need to do is figure out if you're OK with this. If you are just keep the doctype. If not change the doctype. One thing to note is that even if you keep the doctype and the illegal attribute it wont impact your site in any way that it doesnt validate.

In fact your markup might still validate if the data attribute is being added after the page loads - which means that at the point the validation occurs the data attribute wont be there.

Darko Z