JavaScript GUID generator

By Shahram Javey

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.

13 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

Leave a Reply