Implementation example of native JavaScript generation GUID, javascriptguid

Source: Internet
Author: User

Implementation example of native JavaScript generation GUID, javascriptguid

GUID (globally unified identifier) refers to the number generated on a machine, which ensures that all machines in the same time and space are unique. Generally, the platform provides APIs for generating guids. The generation algorithm is very interesting. It uses Ethernet Card addresses, nanoseconds, chip ID codes, and many possible numbers. The unique defect of GUID is that the generated result string is large.

The GUID format is: xxxxxxxx-xxxx-xxxxxxxxxxxx

We all know that GUID is of little use in front-end development, but if you need to insert an ID, and this ID corresponds to the background and other operations that require GUID, for convenience, we can still generate a GUID.

Generally, it is easy to generate a GUID in the background or database languages such as SQL, java, and C #. The front-end does not directly generate a GUID, and you can only write it by yourself. However, because the GUID needs to obtain the address of the Ethernet Card and the number of the time in the nanosecond level. It is difficult for the front-end to obtain this information (please tell me if you know it), and we can simulate and generate a GUID. The Code is as follows:

/*
* Function: generate a GUID code. The GUID consists of 14 or less date and 18 or more hexadecimal random numbers. The GUID has a certain repetition probability, but the repetition probability is extremely low, in theory, the probability of repetition is 1/(16 ^ 18) per 10 ms, that is, 1 out of the 18 power of 16. The probability of repetition is as low as negligible */

Function GUID () {this. date = new Date ();/* determines whether the initialization has been performed. if the following code has been initialized, the following code will not be executed, but will only be executed once */if (typeof this. newGUID! = 'Function') {/* generates the GUID Code */GUID. prototype. newGUID = function () {this. date = new Date (); var guidStr = ''; sexadecimalDate = this. hexadecimal (this. getGUIDDate (), 16); sexadecimalTime = this. hexadecimal (this. getGUIDTime (), 16); for (var I = 0; I <9; I ++) {guidStr + = Math. floor (Math. random () * 16 ). toString (16);} guidStr + = sexadecimalDate; guidStr + = sexadecimalTime; while (guidStr. length <32) {guidStr + = Ma Th. floor (Math. random () * 16 ). toString (16);} return this. formatGUID (guidStr);}/** function: Get the GUID format of the current date, that is, the 8-digit Date: 19700101 * return value: return the string in the GUID date format */GUID. prototype. getGUIDDate = function () {return this. date. getFullYear () + this. addZero (this. date. getMonth () + 1) + this. addZero (this. date. getDay ();}/** function: Get the GUID format of the current time, that is, the 8-digit time, including milliseconds. The millisecond is 2-digit: 12300933 * returned value: returns a string in the 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);}/** function: Add 0 to the front of a single-digit positive integer. If it is a string that can be converted into a non-NaN number, you can also implement the * parameter: the parameter indicates that you want to add a number 0 or a string that can be converted to a number * return value: if the condition is met, the string type after 0 is returned, otherwise, the system returns its own string */GUID. prototype. addZero = function (num) {if (Number (num ). toString ()! = 'Nan '& num> = 0 & num <10) {return '0' + Math. floor (num);} else {return num. toString () ;}/ ** function: Convert the value in y to the value in x. * parameter: 1st parameters indicate the value to be converted; the first parameter indicates the hexadecimal format to be converted. The second parameter is optional, indicating the current hexadecimal number. If this parameter is left blank, the value is 10 * return value: the converted string */GUID is returned. prototype. hexadecimal = function (num, x, y) {if (y! = Undefined) {return parseInt (num. toString (), y ). toString (x);} else {return parseInt (num. toString ()). toString (x) ;}}/** function: format a 32-Bit String in GUID mode. * parameter: 1st parameters: 32-Bit String * return value: A string in the standard GUID format */GUID. prototype. formatGUID = function (guidStr) {var str1 = guidStr. slice (0, 8) + '-', str2 = guidStr. slice (8, 12) + '-', str3 = guidStr. slice (12, 16) + '-', str4 = guidStr. slice (16, 20) + '-', str5 = guidStr. slice (20); return str1 + str2 + str3 + str4 + str5 ;}}}

GUID object

You only need to save it in a JS file and reference it.

Then we only need

Var guid = new GUID ();

Alert (guid. newGUID ());

You can get the GUID code.

The implementation principle is very simple. Here we only use the system time and more than 18 hexadecimal random numbers, and convert the system time to hexadecimal. Although this may be repeated, however, the probability of repetition is extremely low and negligible.


How to bind events to newly generated elements in native javascript

Window. onload = function () {var oBtn = document. getElementById ("btn1"); var oUl = document. getElementById ("ul1"); var aA = oUl. getElementsByTagName ("a"); oBtn. onclick = function () {var oLi = document. createElement ("li"); oLi. innerHTML = '2014 <a href = "javascript:;"> Delete </a> '; oUl. appendChild (oLi); // Add the event aA = oLi to the new. getElementsByTagName ("a"); for (var I = 0; I <aA. length; I ++) {aA [I]. onclick = function () {oUl. removeChild (this. parentNode) ;}}for (var I = 0; I <aA. length; I ++) {aA [I]. onclick = function () {oUl. removeChild (this. parentNode );}}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.