August 23, 2011 | Javascript

Home ยป Javascript » Creating a unique ID in javascript
Creating a unique ID in javascript

While building a project for a customer, I needed a quick way to create a unique identifier using javascript. Here is what I came up with:

function uniqid()
        {
        var newDate = new Date;
        var partOne = newDate.getTime();
        var partTwo = 1 + Math.floor((Math.random()*32767));
        var partThree = 1 + Math.floor((Math.random()*32767));
        var id = partOne + '-' + partTwo + '-' + partThree;
        return id;
        }

 

Then call the function in your code:

var uid = uniqid();
alert(uid);

 

Hope it helps!

 

–=–

Search

Search

Latest

My Music Player

Comments

Comments are closed.

Loading....