JS implementation Click to generate UUID method complete example [based on jQuery]

Time:2019-12-14

The example of this paper describes the method of generating UUID by clicking in JS. To share with you for your reference, as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  < title > www.jb51.net method instance of generating UUID with JS < / Title >
  <script src="https://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script>
    var id = getUuid();
    $(document).ready(function() {
      $("p").click(function() {
        alert(id);
      });
    });
    function getUuid() {
      Var len = 32; // 32 length
      Var radius = 16; // hex
      var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
      var uuid = [], i;
      radix = radix || chars.length;
      if(len) {
        for(i = 0; i < len; i++)uuid[i] = chars[0 | Math.random() * radix];
      } else {
        var r;
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';
        for(i = 0; i < 36; i++) {
          if(!uuid[i]) {
            r = 0 | Math.random() * 16;
            uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
          }
        }
      }
      return uuid.join('');
    }
  </script>
</head>
<body>
<p>Click me to generate UUID. </p>
</body>
</html>

Operation result:

Interested friends can useOnline HTML / CSS / JavaScript code running tool: http://tools.jb51.net/code/htmljsrun test the running effect of the above code.

PS: here are two related online tools for your reference:

Online random number / string generation tool:
http://tools.jb51.net/aideddesign/suijishu

High strength password generator:
http://tools.jb51.net/password/CreateStrongPassword

For more information about jQuery, please refer to the following topics: jQuery operation DOM node method summary, jQuery extension skill summary, jQuery common plug-ins and usage summary, jQuery table operation skill summary, AJAX Usage Summary in jQuery, jQuery common classic special effects summary, jQuery animation and special effects Usage Summary And jQuery selector Usage Summary

I hope this article will be helpful to jQuery programming.