Skip to content

Instantly share code, notes, and snippets.

@dacur
Created February 5, 2015 21:36
Show Gist options
  • Select an option

  • Save dacur/48b2cc626bb35fece6ea to your computer and use it in GitHub Desktop.

Select an option

Save dacur/48b2cc626bb35fece6ea to your computer and use it in GitHub Desktop.
Create a unique GUID. I sometimes use these when I need a unique value; for instance, when I need to add a unique ID to a DIV.
function CreateGuid() {
function _p8(s) {
var p = (Math.random().toString(16) + "000000000").substr(2, 8);
return s ? "-" + p.substr(0, 4) + "-" + p.substr(4, 4) : p;
}
return _p8() + _p8(true) + _p8(true) + _p8();
}
// to create a GUID, just call it like this:
// var id = CreateGuid();
// that's it!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment