Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to store some data in `<a>` anchor tag?

i usually use data-*** to store some data.

<a href="#" data-address="some data">click</a>

i can get it in jquery using

alert($("a").data("address"));

it works fine. but i want to know is it the right way of doing and is there any compatibility issues??

or does i need to use the rel ie:

<a href="#" rel="some data">click</a>

alert($("a").attr("rel"));

i updated a fiddle http://jsfiddle.net/suhailvs/XYZQK/

like image 520
suhailvs Avatar asked Jul 17 '13 05:07

suhailvs


People also ask

What is an anchor tag?

The primary purpose of an anchor tag is to link one page to another page or to a section of the same page. The anchor tag is also known as a HyperLink. Like any other HTML tags, you use the following construct to create an anchor tag: The above anchor tag is a valid HTML tag, but it doesn't do much other than act as a placeholder.

What is the best way to store data in a browser?

There are two main possibilities for browser storage: localStorage and sessionStorage. Any content/data saved to the localStorage object will be available after the browser has been restarted (closed and opened again). In order to save an item to localStorage, you can use the method setItem ().

How to anchor from one page to another in HTML?

This anchoring from one page to another is made possible by the attribute " href ", which can be abbreviated (hypertext reference). The attribute 'href' of the Anchor tag is implemented for defining the address or path to which this hypertext will get linked.

How to store data in localStorage using Todo?

To store data use two parameters as shown. To test the code, clear the console (ctrl + L) and paste the above code with any store name you prefer. localStorage stores data in strings. In our todo application, we use it as follows:


1 Answers

the rel attribute when referrering to an a tag is for search engines to determine the relationship between the document and the one it's linking to.

the data attribute can be used by developers to make custom attributes while storing data in it.

that being said data-*** is the correct way to store some data.

MDN HTML attribute reference

DON'T USE REL FOR CUSTOM DATA STORAGE

COMPATIBILITY CONCERNS

Since the data and rel(referrering to a tag) attributes are suppose to be ignored by browsers, you can use both, either/or. Although it's best practice to use the data tag to store 'data' .


RICH SEARCH RESULTS

if you want search engines to recongize certain links or elements as data describing your content, like page description, publish date, page image, so and so forth, you might want to read up on 'rich search result' and 'rich snippets' from google.

these link will start you out Rich search results or About rich snippets and structured data

like image 139
Jay Harris Avatar answered Oct 20 '22 18:10

Jay Harris