Skip to content

Instantly share code, notes, and snippets.

@atheken
Created April 3, 2012 21:57
Show Gist options
  • Save atheken/2295777 to your computer and use it in GitHub Desktop.
Save atheken/2295777 to your computer and use it in GitHub Desktop.
Guid generator in Javascript.
(function(){
if(!window.String.UUID){
var matcher = /[xy]/g;
var replacer = function(c) {
var r = Math.random()*16|0;
var v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
};
//maybe shouldn't be modifying the global variable, but seems like the best place to put this.
window.String.UUID = function(){
//see http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript for more details.
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(matcher, replacer);
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment