1. 代码一:
  2.  
  3. /*!
  4. Math.uuid.js (v1.4)
  5. http://www.broofa.com
  6. mailto:robert@broofa.com
  7.  
  8. Copyright (c) 2010 Robert Kieffer
  9. Dual licensed under the MIT and GPL licenses.
  10. */
  11.  
  12. /*
  13. * Generate a random uuid.
  14. *
  15. * USAGE: Math.uuid(length, radix)
  16. * length - the desired number of characters
  17. * radix - the number of allowable values for each character.
  18. *
  19. * EXAMPLES:
  20. * // No arguments - returns RFC4122, version 4 ID
  21. * >>> Math.uuid()
  22. * "92329D39-6F5C-4520-ABFC-AAB64544E172"
  23. *
  24. * // One argument - returns ID of the specified length
  25. * >>> Math.uuid(15) // 15 character ID (default base=62)
  26. * "VcydxgltxrVZSTV"
  27. *
  28. * // Two arguments - returns ID of the specified length, and radix. (Radix must be <= 62)
  29. * >>> Math.uuid(8, 2) // 8 character ID (base=2)
  30. * "01001010"
  31. * >>> Math.uuid(8, 10) // 8 character ID (base=10)
  32. * "47473046"
  33. * >>> Math.uuid(8, 16) // 8 character ID (base=16)
  34. * "098F4D35"
  35. */
  36. (function() {
  37. // Private array of chars to use
  38. var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  39.  
  40. Math.uuid = function (len, radix) {
  41. var chars = CHARS, uuid = [], i;
  42. radix = radix || chars.length;
  43.  
  44. if (len) {
  45. // Compact form
  46. for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
  47. } else {
  48. // rfc4122, version 4 form
  49. var r;
  50.  
  51. // rfc4122 requires these characters
  52. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  53. uuid[14] = '4';
  54.  
  55. // Fill in random data. At i==19 set the high bits of clock sequence as
  56. // per rfc4122, sec. 4.1.5
  57. for (i = 0; i < 36; i++) {
  58. if (!uuid[i]) {
  59. r = 0 | Math.random()*16;
  60. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  61. }
  62. }
  63. }
  64.  
  65. return uuid.join('');
  66. };
  67.  
  68. // A more performant, but slightly bulkier, RFC4122v4 solution. We boost performance
  69. // by minimizing calls to random()
  70. Math.uuidFast = function() {
  71. var chars = CHARS, uuid = new Array(36), rnd=0, r;
  72. for (var i = 0; i < 36; i++) {
  73. if (i==8 || i==13 || i==18 || i==23) {
  74. uuid[i] = '-';
  75. } else if (i==14) {
  76. uuid[i] = '4';
  77. } else {
  78. if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
  79. r = rnd & 0xf;
  80. rnd = rnd >> 4;
  81. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  82. }
  83. }
  84. return uuid.join('');
  85. };
  86.  
  87. // A more compact, but less performant, RFC4122v4 solution:
  88. Math.uuidCompact = function() {
  89. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  90. var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
  91. return v.toString(16);
  92. });
  93. };
  94. })();
  95.  
  96. 调用方法:Math.uuid()
  97.  
  98. 代码二:
  99.  
  100. //On creation of a UUID object, set it's initial value
  101. function UUID(){
  102. this.id = this.createUUID();
  103. }
  104.  
  105. // When asked what this Object is, lie and return it's value
  106. UUID.prototype.valueOf = function(){ return this.id; };
  107. UUID.prototype.toString = function(){ return this.id; };
  108.  
  109. //
  110. // INSTANCE SPECIFIC METHODS
  111. //
  112. UUID.prototype.createUUID = function(){
  113. //
  114. // Loose interpretation of the specification DCE 1.1: Remote Procedure Call
  115. // since JavaScript doesn't allow access to internal systems, the last 48 bits
  116. // of the node section is made up using a series of random numbers (6 octets long).
  117. //
  118. var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
  119. var dc = new Date();
  120. var t = dc.getTime() - dg.getTime();
  121. var tl = UUID.getIntegerBits(t,0,31);
  122. var tm = UUID.getIntegerBits(t,32,47);
  123. var thv = UUID.getIntegerBits(t,48,59) + '1'; // version 1, security version is 2
  124. var csar = UUID.getIntegerBits(UUID.rand(4095),0,7);
  125. var csl = UUID.getIntegerBits(UUID.rand(4095),0,7);
  126.  
  127. // since detection of anything about the machine/browser is far to buggy,
  128. // include some more random numbers here
  129. // if NIC or an IP can be obtained reliably, that should be put in
  130. // here instead.
  131. var n = UUID.getIntegerBits(UUID.rand(8191),0,7) +
  132. UUID.getIntegerBits(UUID.rand(8191),8,15) +
  133. UUID.getIntegerBits(UUID.rand(8191),0,7) +
  134. UUID.getIntegerBits(UUID.rand(8191),8,15) +
  135. UUID.getIntegerBits(UUID.rand(8191),0,15); // this last number is two octets long
  136. return tl + tm + thv + csar + csl + n;
  137. };
  138.  
  139. //Pull out only certain bits from a very large integer, used to get the time
  140. //code information for the first part of a UUID. Will return zero's if there
  141. //aren't enough bits to shift where it needs to.
  142. UUID.getIntegerBits = function(val,start,end){
  143. var base16 = UUID.returnBase(val,16);
  144. var quadArray = new Array();
  145. var quadString = '';
  146. var i = 0;
  147. for(i=0;i<base16.length;i++){
  148. quadArray.push(base16.substring(i,i+1));
  149. }
  150. for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){
  151. if(!quadArray[i] || quadArray[i] == '') quadString += '0';
  152. else quadString += quadArray[i];
  153. }
  154. return quadString;
  155. };
  156.  
  157. //Replaced from the original function to leverage the built in methods in
  158. //JavaScript. Thanks to Robert Kieffer for pointing this one out
  159. UUID.returnBase = function(number, base){
  160. return (number).toString(base).toUpperCase();
  161. };
  162.  
  163. //pick a random number within a range of numbers
  164. //int b rand(int a); where 0 <= b <= a
  165. UUID.rand = function(max){
  166. return Math.floor(Math.random() * (max + 1));
  167. };
  168.  
  169. 调用方法:UUID.prototype.createUUID()

javascript 生成UUID的更多相关文章

  1. javascript 生成 uuid

    全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) . GUID是一种由算法生成的二进制长度 ...

  2. js,javascript生成 UUID的四种方法

    全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) . GUID是一种由算法生成的二进制长度 ...

  3. 使用javascript生成的植物显示过程特效

    查看效果:http://keleyi.com/keleyi/phtml/html5/33.htm .NET版本:http://keleyi.com/a/bjac/66mql4bc.htm 完整HTML ...

  4. Java 生成 UUID

    1.UUID 简介 UUID含义是通用唯一识别码 (Universally Unique Identifier),这是一个软件建构的标准,也是被开源软件基金会 (Open Software Found ...

  5. php生成UUID

    UUID含义是 通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, O ...

  6. 【原创】网站抓包HttpWebRequest不返回Javascript生成的Cookie的解决办法

    前言: 最近在做中国移动爬虫的过程中,首先遇到的就是 在某个请求中,有一个名为“WT_PFC"的cookie键值是由前端JavaScript生成的,没有进入到HttpWebResponse中 ...

  7. JS生成UUID的方法实例

    <!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1. ...

  8. linux c 生成uuid

    /********方法一**********/#include <stdio.h> #include <stdlib.h> #include <string.h> ...

  9. java 生成UUID

    UUID(Universally Unique Identifier)全局唯一标识符,是一个128位长的数字,一般用16进制表示. 算法的核心思想是结合机器的网卡.当地时间.一个随即数来生成UUID, ...

随机推荐

  1. Swift - IBOutlet返回nil(fatal error: unexpectedly found nil while unwrapping an Optional value)

    在Swift 中 ViewController 默认构造方法不关联同名的xib文件 在使用OC的时候,调用ViewController的默认构造函数,会自动关联到一个与ViewController名字 ...

  2. c笔试题(1)

    1.sizeof和strlen的区别 #include<stdio.h> #include<string.h> int main() { char a[10] = " ...

  3. Qt5.4静态编译方法

    静态编译,就是编译器在编译可执行文件的时候,将可执行文件需要调用的对应动态链接库(.so或.lib)中的部分提取出来,链接到可执行文件中去,使可执行文件在运行的时候不依赖于动态链接库.这样就可以发布单 ...

  4. Mysql学习(慕课学习笔记8)插入、更新、删除记录

    插入记录 Insert[]into] tb1_name[(col_name,…..)] 自动编号的字段,可以用values default Default 可以赋予默认值 INSERT USERS V ...

  5. 本地windows下PHP连接远程oracle遇到的诸多问题

    任务目的:本地windows下PHP连接远程服务器下的oracle. 必须必须 确定服务器的数据库版本,如果本地的驱动和对方服务器版本不一致,会导致许多报错. 已知的oracle版本  分为 32位的 ...

  6. pyqt5:标签显示文本框内容

    文本框(lineEdit)输入文本,标签(label)就会显示文本框的内容. 原理如下: 输入文本时,lineEdit控件发射信号textChanged(),label收到后触发setText()槽. ...

  7. Intent系列讲解---Intent简介以及相关属性

    一.Intent简介 Intent中文是"意图,意向",它是Android中四大组件通讯的纽带,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Androi ...

  8. WCF-IIS-PDA

    PDA调用WCF 一 IIS托管WCF 项目从开始是用IIS托管的WCF,但一直出错,到最后也没有搞定,希望哪位大神知道的话可以指点. 错误如下: There was no endpoint list ...

  9. Codeforces 527D Clique Problem

    http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...

  10. VMware虚拟机相关文件问题

    .vmx VM的配置文件 .vmdk VM的虚拟硬盘 .vmsd VM快照和相关联的vmdk的字典文件 .vswap 虚拟交换文件 .nvram 虚拟机的BIOS信息.VM会生成VMX, VMDK, ...