I would like to know what would be the correct way to generate an unique identifier string from an object say, a div for example. It should be
- Unique – well, something like pre 1.4 jQuery.data(element) but that uses cache, right?
- Stable – Remains the same as such after refresh and so, thus not based on Date/Time or cache.
- Generate from the contents of the element, For example, duplicating that element should basically give the same identifier.
- A string.
Regular basic ways to generate a default id, like increments, arrays is not sufficient here. Please share your wisdom.
Thanks in advance.
EDIT: Not for the element attribute ID please.
You could get the element contents (including the element itself) has a string, then implement something like String.hashCode() method as implemented in the java jdk.
The hash code for a String object is computed as
using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
Depending on your implementation/task/use of that, it may be preferable to implement this function on the server side though