How to generate a unique id in JavaScript
PHP makes it pretty easy to generate a unique it with the uniqid() command. JavaScript doesn't have that command, but using the getTime() function of the Date object we can do almost the same thing.
JavaScript:
-
function uniqid()
-
{
-
var newDate = new Date;
-
return newDate.getTime();
-
}
The function returns the number of milliseconds since January 1, 1970.
Question, Comments...
Do you have more questions. Please either leave a comment below or join us in our new forum.
[...] Forums, Blogs, Wikis News on forums, blogs, wikis, cms, etc « How to generate a unique id in JavaScript [...]
Found a problem if you’re using this to generate unique IDs in rapid succession. It’s possible that two IDs can be generated within the same millisecond and therefore will have the same “unique” ID. Instead try this technique that increments a variable to keep the ID unique.
Since different systems can’t ever seem to get their dates straight, other experts proffer something more like this. Source reported as Flanagan (Rhino) Javascript book, page 131 of the 5th edition. (I threw in the optional argument check against 0 to reset it if necessary.)
Usage is standard: getUniqueId(); or getUniqueId(0); to reset;
var getUniqueId = (function() {var id=0;return function() {if (arguments[0]==0) {id=0;return 1;} else return id++;}})();
I would like to work in a time stamp feature toward the concept of an ID as well as class names and such, but that may have to wait until 64-bit systems are entrenched.
Whoops: mistyped:
var getUniqueId = (function() {var id=0;return function() {if (arguments[0]==0) {id=1;return 0;} else return id++;}})();
I wound up not even using this on the project I put it together for originally. It was just easier to declare a variable at the top of the JS file and ++ it as needed inside the functions.
Since different systems can’t ever seem to get their dates straight, other experts proffer something more like this. Source reported as Flanagan (Rhino) Javascript book, page 131 of the 5th edition. (I threw in the optional argument check against 0 to reset it if necessary.)
Usage is standard: getUniqueId(); or getUniqueId(0); to reset;
var getUniqueId =
(function()
{
var id=0;
return function()
{
if (arguments[0]==0) id=0;
return id++;
}
}
)();
I would like to work in a time stamp feature toward the concept of an ID as well as class names and such, but that may have to wait until 64-bit systems are entrenched.
function uniqid()
{
var id = 0;
var flag=true;
while(flag)
{
if(typeof(document.getElementById(id))==”undefined”)
return id;
else
i++;
}
}
The above code is useful, if we need to create element in modification time beacuse the element is already been used before submition of form.
it’s not that unique, put this:
document.write(uniqid()+”");
document.write(uniqid()+”");
document.write(uniqid()+”");
and the result is the same value.
it’s not unique, it’s just to get the milisecond
I really don’t use this anymore, although it’s more because creating a DateTime object seemed heavier than need be. What I do now is just create a global variable and always reference it with varName++