用javascript生成UUID

有时需要在前端生成UUID,可以在jsp中引用内容如下的文件UUID.js。 

使用时直接用new生成即可 
Js代码   收藏代码
  1. var uuidId = new UUID();  



UUID.js 
Js代码   收藏代码
  1. /* 
  2.  *  
  3.  * uuid.js - Version 0.3 JavaScript Class to create a UUID like identifier 
  4.  *  
  5.  * Copyright (C) 2006-2008, Erik Giberti (AF-Design), All rights reserved. 
  6.  *  
  7.  * This program is free software; you can redistribute it and/or modify it under 
  8.  * the terms of the GNU General Public License as published by the Free Software 
  9.  * Foundation; either version 2 of the License, or (at your option) any later 
  10.  * version. 
  11.  *  
  12.  * This program is distributed in the hope that it will be useful, but WITHOUT 
  13.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  14.  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
  15.  * details. 
  16.  *  
  17.  * You should have received a copy of the GNU General Public License along with 
  18.  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
  19.  * Place, Suite 330, Boston, MA 02111-1307 USA 
  20.  *  
  21.  * The latest version of this file can be downloaded from 
  22.  * http://www.af-design.com/resources/javascript_uuid.php 
  23.  *  
  24.  * HISTORY: 6/5/06 - Initial Release 5/22/08 - Updated code to run faster, 
  25.  * removed randrange(min,max) in favor of a simpler rand(max) function. Reduced 
  26.  * overhead by using getTime() method of date class (suggestion by James Hall). 
  27.  * 9/5/08 - Fixed a bug with rand(max) and additional efficiencies pointed out 
  28.  * by Robert Kieffer http://broofa.com/ 
  29.  *  
  30.  * KNOWN ISSUES: - Still no way to get MAC address in JavaScript - Research into 
  31.  * other versions of UUID show promising possibilities (more research needed) - 
  32.  * Documentation needs improvement 
  33.  *  
  34.  */  
  35.   
  36. // On creation of a UUID object, set it's initial value  
  37. function UUID() {  
  38.     this.id = this.createUUID();  
  39. }  
  40.   
  41. // When asked what this Object is, lie and return it's value  
  42. UUID.prototype.valueOf = function() {  
  43.     return this.id;  
  44. }  
  45. UUID.prototype.toString = function() {  
  46.     return this.id;  
  47. }  
  48.   
  49. //  
  50. // INSTANCE SPECIFIC METHODS  
  51. //  
  52.   
  53. UUID.prototype.createUUID = function() {  
  54.     //  
  55.     // Loose interpretation of the specification DCE 1.1: Remote Procedure Call  
  56.     // described at  
  57.     // http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagtcjh_37  
  58.     // since JavaScript doesn't allow access to internal systems, the last 48  
  59.     // bits  
  60.     // of the node section is made up using a series of random numbers (6 octets  
  61.     // long).  
  62.     //    
  63.     var dg = new Date(1582, 10, 15, 0, 0, 0, 0);  
  64.     var dc = new Date();  
  65.     var t = dc.getTime() - dg.getTime();  
  66.     var tl = UUID.getIntegerBits(t, 0, 31);  
  67.     var tm = UUID.getIntegerBits(t, 32, 47);  
  68.     var thv = UUID.getIntegerBits(t, 48, 59) + '1'// version 1, security version is 2  
  69.     var csar = UUID.getIntegerBits(UUID.rand(4095), 0, 7);  
  70.     var csl = UUID.getIntegerBits(UUID.rand(4095), 0, 7);  
  71.   
  72.     // since detection of anything about the machine/browser is far to buggy,  
  73.     // include some more random numbers here  
  74.     // if NIC or an IP can be obtained reliably, that should be put in  
  75.     // here instead.  
  76.     var n = UUID.getIntegerBits(UUID.rand(8191), 0, 7)  
  77.             + UUID.getIntegerBits(UUID.rand(8191), 8, 15)  
  78.             + UUID.getIntegerBits(UUID.rand(8191), 0, 7)  
  79.             + UUID.getIntegerBits(UUID.rand(8191), 8, 15)  
  80.             + UUID.getIntegerBits(UUID.rand(8191), 0, 15); // this last number is two octets long  
  81.     return tl + tm + thv + csar + csl + n;  
  82. }  
  83.   
  84. //  
  85. // GENERAL METHODS (Not instance specific)  
  86. //  
  87.   
  88. // Pull out only certain bits from a very large integer, used to get the time  
  89. // code information for the first part of a UUID. Will return zero's if there  
  90. // aren't enough bits to shift where it needs to.  
  91. UUID.getIntegerBits = function(val, start, end) {  
  92.     var base16 = UUID.returnBase(val, 16);  
  93.     var quadArray = new Array();  
  94.     var quadString = '';  
  95.     var i = 0;  
  96.     for (i = 0; i < base16.length; i++) {  
  97.         quadArray.push(base16.substring(i, i + 1));  
  98.     }  
  99.     for (i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {  
  100.         if (!quadArray[i] || quadArray[i] == '')  
  101.             quadString += '0';  
  102.         else  
  103.             quadString += quadArray[i];  
  104.     }  
  105.     return quadString;  
  106. }  
  107.   
  108. // Replaced from the original function to leverage the built in methods in  
  109. // JavaScript. Thanks to Robert Kieffer for pointing this one out  
  110. UUID.returnBase = function(number, base) {  
  111.     return (number).toString(base).toUpperCase();  
  112. }  
  113.   
  114. // pick a random number within a range of numbers  
  115. // int b rand(int a); where 0 <= b <= a  
  116. UUID.rand = function(max) {  
  117.     return Math.floor(Math.random() * (max + 1));  
  118. }  
  119.   
  120. // end of UUID class file  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值