JavaScript GUID generator


At times it is handy to create a guid on the client. Here is an example of how to do this. Just call function guid on the page’s JavaScript and use it as an input field of a form submission, for example.


function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

Note. strictly speaking this is a nothing more than a random number that looks a GUID. For session based activities, it may be useful; but for anything more permanent, use a proper GUID generator on the server.

22 Responses to “JavaScript GUID generator”

  1. UUID Generator « n o t e 1 9 . c o m Says:

    [...] UUID Generator Every once in while I need a UUID. So here is a hosted version (based on this). [...]

  2. Raja Sekhar Says:

    Hi,

    Thanx 4 d code… its really useful…

  3. Andrew Albrecht Says:

    That is really great. Thank you.
    This is way better than other, far more involved and complicated, solutions that I have seen. Nice work.

  4. Frank Wirth Says:

    Thanks for the nice code, but just for the correctness: this is no Global Unique Identifier, it is just a random generator with the output that looks like a GUID. But may be also useful ;-)

  5. Cohen Says:

    additionally to what frank says: you may get guid’s in the wrong space if you read about guids in wikipedia: http://en.wikipedia.org/wiki/Globally_Unique_Identifier
    You’ll see that there are different types of guids. quote: ‘One to three of the most significant bits of the second byte in Data 4 define the type variant of the GUID’. Something you don’t take in account in the above code. Still very usefull to generate a random guid-like number. Thanx for sharing.

  6. Frank Says:

    Not as complicated as other. It also simply works. Thanks!

  7. sj Says:

    Thank you everyone for your comments. I agree that is not in the strict sense a Globally Unique IDentifier. I use it in areas where I need a unique identifier for session based activities. For example, I used this for associating a GUID with file uploads when using Flex (much like a session ID), where it may be more convenient to use client side GUID generation.

  8. ASpeak » Generate Random GUID by script Says:

    [...] JScript or VBScript but couldn’t find something useful. I am not looking for re-writing a logic to generate a GUID with a specified pattern with all the scemantics. I just want a simple object [...]

  9. Vivek Says:

    Awesome man! Thanks a bunch :-)

  10. matt Says:

    Thanks mate, simple easy to use and works!

  11. Robert K Says:

    for anyone who’s interested, there’s an RFC4122-compliant GUID/UUID generator function available at http://www.broofa.com/blog/?p=151 . The demo/test is available at http://www.broofa.com/Tools/Math.uuid.htm

  12. GUID generator in JavaScript « Joshua Doodnauth’s WebLife Says:

    [...] to sj at note19.com, I don’t know exactly how unique the number will continue to be, I am still open to any other [...]

  13. izz Says:

    hey can some one tell me how to validate the GUID through a javascript

  14. Matze Says:

    Thanks for this, just what I was looking for :-)

  15. Jp Says:

    Thanks, i used it to make a jQuery plugin like prototype’s identify

    http://plugins.jquery.com/project/identifyII

  16. Mike Says:

    Not a GUID but it helps me to generate GUID-like strings. Thanks!

  17. Steven Says:

    Sweet – so simple! Thanks man!

  18. JavaScript GUID generator Says:

    [...] http://note19.com/2007/05/27/javascript-guid-generator/ Comments [0]Digg it!Facebook [...]

  19. webkatalog Says:

    is great thanks

  20. Generate a GUID Using Javascript « Arcanum Obscura Says:

    [...] Software — pdgowdy @ 1:25 pm If you need to generate a GUID using javascript,¬†please see¬†this post. In case that post is down, below is the relevant [...]

  21. Sermormo Says:

    Thanks…

  22. How to create a GUID / UUID in Javascript? | Everyday I'm coding Says:

    [...] just random numbers that look like GUIDs? It’s easy enough to generate random numbers. From http://note19.com/2007/05/27/javascript-guid-generator/ (after some clean-up for clarity’s [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: