Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using custom data attributes produce browser compatibility issues?

I have to choose between custom data tags or ids. I would like to choose custom data tags, but I want to be sure that they do not cause browser compatibility issues for the most widely used browsers today.

I'm using jQuery 1.6 and my particular scenario involves a situation where I need to reference a commentId for several actions.

<div data-comment-id="comment-1" id="comment-1">
   <a class="foo"></a>
</div>

It's easier to extract data tags in jQueryin: $('foo').data('commentId');

Extract a substring from the id seems a bit complicated and could break for one reason or another: <a id="comment-1"

Are there any sweeping merits or fatal flaws for either approach?

like image 853
Mohamad Avatar asked Jun 03 '11 02:06

Mohamad


People also ask

What is the use of custom data attributes?

Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements. These attributes are not intended for use by software that is independent of the site that uses the attributes.

How does JavaScript handle browser compatibility issues?

Use browser developer tools/dev tools that help to debug JavaScript. In most browsers, the JavaScript console will flag and report errors in code. Use the Console API to allow JS source code to communicate with the browser's JS console. Console API's most widely used feature is console.

Can you use custom HTML attributes?

You can add custom HTML attributes to pages and page elements, which are rendered on the HTML tag of the page element. For example, this is useful when working with third-party frameworks that render page elements differently based on certain attributes.


2 Answers

I would advise in favor of data attributes for the following reasons:

  • ids need to be unique document-wide. Thus they are limited in the semantics they can carry
  • you can have multiple data-attributes per element

and probably less relevant in your case:

  • changing ids might break idrefs

However, I'm not sure whether I understand your specs completely as extracting the element id in jQuery is as trivial as getting the data attribute: $('.foo').attr('id');.

You might be interested in Caniuse.com, a browser compatibility site for web technologies.

If XHTML is an issue to you, you might also be interested in how to use custom data attributes in XHTML: see here for a discussion on SO and here for an XHTML-compatible approach using namespaces.

like image 166
collapsar Avatar answered Oct 30 '22 11:10

collapsar


this guy says data attibutes work on IE6.

like image 33
Tom Avatar answered Oct 30 '22 12:10

Tom