A GUID (global uniform identifier) is a number generated on a machine that guarantees that all machines in the same time and space are unique. Typically, the platform provides the API to generate the GUID. The generation algorithm is interesting, using the Ethernet card address, nanosecond time, chip ID code and many possible numbers. The only drawback to the GUID is that the resulting string will be larger.
The format of the GUID is: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
As you know, GUIDs are not very useful in front-end development, but if you need to insert an ID, and this ID corresponds to other operations that require a GUID, for convenience, we can still generate a GUID.
It is generally easy to generate GUIDs in the background or database language such as SQL, Java, C #, etc., and the front-end does not directly generate GUIDs, only one by itself. However, because the GUID needs to obtain the address of the Ethernet card, as well as the nanosecond-level time and other numbers. While the front-end access to this information is more difficult (know the children's shoes please tell me), and we can simulate the implementation of the generated GUID, the code is as follows:
1 /*2 * Function: Generate a GUID code, where the GUID is 14 of the following date time and 18 more than 16 binary random number, the GUID has a certain repetition probability, but the repetition probability is very low, the theoretical repetition probability is 1/(16^18) per 10ms, that is 16 18 square 1, The repetition probability is low to negligible3 * Disclaimer: This code is dedicated to the author's learning, such as the user in the course of use due to the loss caused by code problems, and the author has no relationship4 * Date: September 4, 20145 * WYC6 * Http://www.cnblogs.com/wuyuchang7 */8 9 Ten functionGUID () { One This. Date =NewDate (); A - /*If the following code is initialized, the following code is no longer executed, and only one time is actually executed*/ - if(typeof This. newguid! = ' function ') { the - /*Generate GUID Code*/ -GUID.prototype.newGUID =function() { - This. Date =NewDate (); + varGuidstr = ' '; -Sexadecimaldate = This. Hexadecimal ( This. Getguiddate (), 16); +Sexadecimaltime = This. Hexadecimal ( This. Getguidtime (), 16); A for(vari = 0; I < 9; i++) { atGuidstr + = Math.floor (Math.random () *16). toString (16); - } -Guidstr + =sexadecimaldate; -Guidstr + =Sexadecimaltime; - while(Guidstr.length < 32) { -Guidstr + = Math.floor (Math.random () *16). toString (16); in } - return This. Formatguid (GUIDSTR); to } + - /* the * Function: Gets the GUID format of the current date, which is the 8-digit date: 19700101 * * Return Value: A note string that returns a GUID date format $ */Panax NotoginsengGUID.prototype.getGUIDDate =function() { - return This. Date.getfullyear () + This. Addzero ( This. Date.getmonth () + 1) + This. Addzero ( This. Date.getday ()); the } + A /* the * Function: Gets the GUID format of the current time, that is, 8-digit time, including milliseconds, 2 digits in milliseconds: 12300933 + * Return Value: A note string that returns a GUID date format - */ $GUID.prototype.getGUIDTime =function() { $ return This. Addzero ( This. Date.gethours ()) + This. Addzero ( This. Date.getminutes ()) + This. Addzero ( This. Date.getseconds ()) + This. Addzero (parseint ( This. Date.getmilliseconds ()/10 )); - } - the /* - * Function: Adds 0 to a positive integer of one number, or to a string that can be converted to a non-Nan numberWuyi * Parameters: The parameter indicates that you are ready to add a 0 number or a string that can be converted to a number the * Return Value: Returns the string type after adding 0 if the condition is met, otherwise returns its own strings - */ WuGUID.prototype.addZero =function(num) { - if(num). toString ()! = ' NaN ' && num >= 0 && num < 10) { About return' 0 ' +Math.floor (num); $}Else { - returnnum.tostring (); - } - } A + /* the * Function: Converts the value of y into x -binary value - * Parameters: The 1th parameter represents the value to be converted, the 2nd parameter represents the binary to be converted, and the 3rd parameter is optional, indicating the current number of digits, if not written $ * Return value: Returns the converted string the */ theGUID.prototype.hexadecimal =function(num, x, y) { the if(Y! =undefined) { the returnparseint (num.tostring (), y). toString (x); -}Else { in returnparseint (Num.tostring ()). toString (x); the } the } About the /* the * Function: Format 32-bit string as GUID pattern string the * Parameters: The 1th parameter represents a 32-bit string + * return value: A string in the standard GUID format - */ theGUID.prototype.formatGUID =function(guidstr) {Bayi varstr1 = Guidstr.slice (0, 8) + '-', theSTR2 = Guidstr.slice (8, 12) + '-', theSTR3 = Guidstr.slice (12, 16) + '-', -STR4 = Guidstr.slice (16, 20) + '-', -STR5 = Guidstr.slice (20); the returnSTR1 + str2 + str3 + STR4 +STR5; the } the } the}
GUID Object
Just save it in a JS file and refer to it.
Then we just need to
var guid = new GUID ();
Alert (Guid.NewGuid ());
To get the GUID code.
The implementation of the principle is very simple, here is only the system time with more than 18 hexadecimal random number composition, and the system time to convert to 16, so although it is still possible to repeat, but the probability of repetition is very low, negligible.
The above for I write the method of generating GUIDs, if there is a better way to tell me, thank you!
Implementation of native JavaScript generation GUID