Create GUID / UUID in JavaScript?

I'm trying to create globally-unique identifiers in JavaScript. I'm not sure what routines are available on all browsers, how random and seeded the built-in random number generator is, etc..

The GUID / UUID should be at least 32 characters and should stay in the ASCII range to avoid trouble when passing them around.

Text Copied From stackoverflow.com
on : Fri, Sep 19, 2008 Views : 1146118

Accepted Answer

There have been a couple attempts at this. The question is: do you want actual GUIDs, or just random numbers that look like GUIDs? It's easy enough to generate random numbers.



function guid() 
{
   
function s4() 
{

  return Math.floor((1 + Math.random()) * 0x10000)
    .toString(16)
    .substring(1);
   
}   return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
 

However, note that such values are not genuine GUIDs.

There's no way to generate real GUIDs in Javascript, because they depend on properties of the local computer that browsers do not expose. You'll need to use OS-specific services like ActiveX: http://p2p.wrox.com/topicindex/20339.htm

Edit: not correct - RFC4122 allows random (version 4) GUIDs. See other answers for specifics.

Note: the provided code snippet does not follow RFC4122 which requires that the version (4) has to be integrated into the generated output string. Do not use this answer if you need compliant GUIDs.

Use:

var uuid = guid();
 

Demo:



function guid() 
{
   return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  s4() + '-' + s4() + s4() + s4();
 
}  
function s4() 
{
   return Math.floor((1 + Math.random()) * 0x10000)
  .toString(16)
  .substring(1);
 
}  document.getElementById('jsGenId').addEventListener('click', 
function() 
{
   document.getElementById('jsIdResult').value = guid();
 
})
input 
{
 font-family: monospace;
 
}
<button id=jsGenId type=button>Generate GUID</button> <br> <input id=jsIdResult type=text placeholder=Results will be placed here... readonly size=40/>

Text Copied From stackoverflow.com
on : Fri, Sep 19, 2008

Ask Question

If you want to ask question you must LogIn or SignUp
Login SignUp

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp