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.
October 7, 2007 at 9:20 pm |
[...] UUID Generator Every once in while I need a UUID. So here is a hosted version (based on this). [...]
May 12, 2008 at 3:06 pm |
Hi,
Thanx 4 d code… its really useful…
May 22, 2008 at 7:57 pm |
That is really great. Thank you.
This is way better than other, far more involved and complicated, solutions that I have seen. Nice work.
June 10, 2008 at 11:01 am |
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
July 10, 2008 at 7:09 am |
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.
July 16, 2008 at 4:02 pm |
Not as complicated as other. It also simply works. Thanks!
July 22, 2008 at 4:46 am |
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.
August 12, 2008 at 7:40 pm |
[...] 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 [...]
September 24, 2008 at 3:47 pm |
Awesome man! Thanks a bunch
October 23, 2008 at 4:15 am |
Thanks mate, simple easy to use and works!
November 19, 2008 at 9:57 pm |
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
December 3, 2008 at 2:12 am |
[...] 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 [...]
December 31, 2008 at 7:15 am |
hey can some one tell me how to validate the GUID through a javascript
July 28, 2009 at 10:02 pm |
Thanks for this, just what I was looking for
August 6, 2009 at 11:40 am |
Thanks, i used it to make a jQuery plugin like prototype’s identify
http://plugins.jquery.com/project/identifyII
August 13, 2009 at 1:21 pm |
Not a GUID but it helps me to generate GUID-like strings. Thanks!
September 1, 2009 at 4:29 pm |
Sweet – so simple! Thanks man!
January 6, 2010 at 5:44 pm |
[...] http://note19.com/2007/05/27/javascript-guid-generator/ Comments [0]Digg it!Facebook [...]
June 3, 2010 at 12:03 pm |
is great thanks
August 23, 2010 at 9:25 pm |
[...] 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 [...]
June 6, 2011 at 7:14 pm |
Thanks…
March 12, 2013 at 9:11 am |
[...] 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 [...]