javascript 生成UUID

代码一: 

/*!
Math.uuid.js (v1.4)
 http://www.broofa.com
 mailto:[email protected]

 Copyright (c) 2010 Robert Kieffer
 Dual licensed under the MIT and GPL licenses.
 */

 /*
  * Generate a random uuid.
  *
  * USAGE: Math.uuid(length, radix)
  *   length - the desired number of characters
  *   radix  - the number of allowable values for each character.
  *
  * EXAMPLES:
  *   // No arguments  - returns RFC4122, version 4 ID
  *   >>> Math.uuid()
  *   "92329D39-6F5C-4520-ABFC-AAB64544E172"
  *
  *   // One argument - returns ID of the specified length
  *   >>> Math.uuid(15)     // 15 character ID (default base=62)
  *   "VcydxgltxrVZSTV"
  *
  *   // Two arguments - returns ID of the specified length, and radix. (Radix must be <= 62)
  *   >>> Math.uuid(8, 2)  // 8 character ID (base=2)
  *   "01001010"
  *   >>> Math.uuid(8, 10) // 8 character ID (base=10)
  *   "47473046"
  *   >>> Math.uuid(8, 16) // 8 character ID (base=16)
  *   "098F4D35"
  */
 (function() {
   // Private array of chars to use
   var CHARS = ‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz‘.split(‘‘);

   Math.uuid = function (len, radix) {
     var chars = CHARS, uuid = [], i;
     radix = radix || chars.length;

     if (len) {
       // Compact form
       for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
     } else {
       // rfc4122, version 4 form
       var r;

       // rfc4122 requires these characters
       uuid[8] = uuid[13] = uuid[18] = uuid[23] = ‘-‘;
       uuid[14] = ‘4‘;

       // Fill in random data.  At i==19 set the high bits of clock sequence as
       // per rfc4122, sec. 4.1.5
       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(‘‘);
   };

   // A more performant, but slightly bulkier, RFC4122v4 solution.  We boost performance
   // by minimizing calls to random()
   Math.uuidFast = function() {
     var chars = CHARS, uuid = new Array(36), rnd=0, r;
     for (var i = 0; i < 36; i++) {
       if (i==8 || i==13 ||  i==18 || i==23) {
         uuid[i] = ‘-‘;
       } else if (i==14) {
         uuid[i] = ‘4‘;
       } else {
         if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
         r = rnd & 0xf;
         rnd = rnd >> 4;
         uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
       }
     }
     return uuid.join(‘‘);
   };

   // A more compact, but less performant, RFC4122v4 solution:
   Math.uuidCompact = function() {
     return ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx‘.replace(/[xy]/g, function(c) {
       var r = Math.random()*16|0, v = c == ‘x‘ ? r : (r&0x3|0x8);
       return v.toString(16);
     });
   };
 })(); 

调用方法:Math.uuid() 

代码二: 

//On creation of a UUID object, set it‘s initial value
 function UUID(){
     this.id = this.createUUID();
 }

 // When asked what this Object is, lie and return it‘s value
 UUID.prototype.valueOf = function(){ return this.id; };
 UUID.prototype.toString = function(){ return this.id; };

 //
 // INSTANCE SPECIFIC METHODS
 //
 UUID.prototype.createUUID = function(){
     //
     // Loose interpretation of the specification DCE 1.1: Remote Procedure Call
     // since JavaScript doesn‘t allow access to internal systems, the last 48 bits
     // of the node section is made up using a series of random numbers (6 octets long).
     //
     var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
     var dc = new Date();
     var t = dc.getTime() - dg.getTime();
     var tl = UUID.getIntegerBits(t,0,31);
     var tm = UUID.getIntegerBits(t,32,47);
     var thv = UUID.getIntegerBits(t,48,59) + ‘1‘; // version 1, security version is 2
     var csar = UUID.getIntegerBits(UUID.rand(4095),0,7);
     var csl = UUID.getIntegerBits(UUID.rand(4095),0,7);

     // since detection of anything about the machine/browser is far to buggy,
     // include some more random numbers here
     // if NIC or an IP can be obtained reliably, that should be put in
     // here instead.
     var n = UUID.getIntegerBits(UUID.rand(8191),0,7) +
             UUID.getIntegerBits(UUID.rand(8191),8,15) +
             UUID.getIntegerBits(UUID.rand(8191),0,7) +
             UUID.getIntegerBits(UUID.rand(8191),8,15) +
             UUID.getIntegerBits(UUID.rand(8191),0,15); // this last number is two octets long
     return tl + tm  + thv  + csar + csl + n;
 };

 //Pull out only certain bits from a very large integer, used to get the time
 //code information for the first part of a UUID. Will return zero‘s if there
 //aren‘t enough bits to shift where it needs to.
 UUID.getIntegerBits = function(val,start,end){
     var base16 = UUID.returnBase(val,16);
     var quadArray = new Array();
     var quadString = ‘‘;
     var i = 0;
     for(i=0;i<base16.length;i++){
         quadArray.push(base16.substring(i,i+1));
     }
     for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){
         if(!quadArray[i] || quadArray[i] == ‘‘) quadString += ‘0‘;
         else quadString += quadArray[i];
     }
     return quadString;
 };

 //Replaced from the original function to leverage the built in methods in
 //JavaScript. Thanks to Robert Kieffer for pointing this one out
 UUID.returnBase = function(number, base){
     return (number).toString(base).toUpperCase();
 };

 //pick a random number within a range of numbers
 //int b rand(int a); where 0 <= b <= a
 UUID.rand = function(max){
     return Math.floor(Math.random() * (max + 1));
 }; 

调用方法:UUID.prototype.createUUID()
时间: 2024-10-07 21:17:05

javascript 生成UUID的相关文章

Javascript生成UUID(方法2)

可以先直接在线生成一个uuid,预览一下, 这个网站不错,可以选择uuid version,还可以直接去掉中划线,好用的不得了啊,哈哈http://www.uuid.online/ //On creation of a UUID object, set it's initial value function UUID(){ this.id = this.createUUID(); } // When asked what this Object is, lie and return it's v

javascript 生成 uuid

全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) . GUID是一种由算法生成的二进制长度为128位的数字标识符.GUID 的格式为"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",其中的 x 是 0-9 或 a-f 范围内的一个32位十六进制数.在理想情况下,任何计算机和计算机集群都不会生成两个相同的GUID. GUID 的总数达到了2^128(3

javascript 生成UUID(方法1)

可以先直接在线生成一个uuid,预览一下, 这个网站不错,可以选择uuid version,还可以直接去掉中划线,好用的不得了啊,哈哈http://www.uuid.online/ /*! Math.uuid.js (v1.4) http://www.broofa.com mailto:[email protected] Copyright (c) 2010 Robert Kieffer Dual licensed under the MIT and GPL licenses. */ /* *

Java,Javascript,Oracle开发生成UUID或GUID方法总结

1.Java开发生成UUID或GUID方法 // 36位的,带中划线 java.util.UUID.randomUUID().toString(); // 32位的,去掉中划线 java.util.UUID.randomUUID().toString().replaceAll("-", ""); 2.Javascript开发生成UUID或GUID方法 /* 参数middle_line true:带中划线 false不带中划线*/ function newGuid(m

javascript生成表格增删改查 JavaScript动态改变表格单元格内容 动态生成表格 JS获取表格任意单元格 javascript如何动态删除表格某一行

jsp页面表格布局Html代码 <body > <center> <input type="text" value="111" id="mytext"> <table border="1" width="60%" id="mytable"> <tr> <td id="td1">第一行第一列<

Extjs生成UUID

好久没写博文了,最近公司接了个项目要求用Extjs, 个人觉得Extjs优点蛮多滴... 由于特殊需求需要JS处生成UUID,之前原生JS引用的是第三方的JS(源自github) 现在有了Extjs只需要一句话就分分钟搞定了: Ext.data.IdGenerator.get('uuid').generate(); 另外:貌似这个ID生成期可以自定义,这个打算以后研究一下.

Python3抓取javascript生成的html网页

用urllib等抓取网页,只能读取网页的静态源文件,而抓不到由javascript生成的内容. 究其原因,是因为urllib是瞬时抓取,它不会等javascript的加载延迟,所以页面中由javascript生成的内容,urllib读取不到. 那由javascript生成的内容就真的没有办法读取了吗?非也! 本文要介绍一个python库:selenium,目前最新版本是 2.44.0 先安装: pip install -U selenium 下面用三个例子来说明其用法: [例0] 打开一个Fir

如何用javascript获取UUID

由于javascript是单线程的东西,所以我们放一个累加变量作为id也不会重复. 但其实javascript本身提供了可以获得唯一id的东东.还记得setTimeout()方法会返回一个唯一id用来后来清除定时器,我们不妨就借用这个id吧. function getUUID() { var id = setTimeout('0'); clearTimeout(id); return id; }; 如何用javascript获取UUID,布布扣,bubuko.com

Java 生成 UUID

阅读目录 1.UUID 简介 2.UUID 组成 3.项目实战 回到顶部 1.UUID 简介 UUID含义是通用唯一识别码 (Universally Unique Identifier),这是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OSF) 的组织应用在分布式计算环境 (Distributed Computing Environment, DCE) 领域的一部分. UUID 的目的,是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要