码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript生成GUID的方法

时间:2014-10-22 19:51:39      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   os   ar   java   for   strong   sp   

一、生成GUID的方法一
 
 
JScript 代码   复制
bubuko.com,布布扣bubuko.com,布布扣
function guid() {
bubuko.com,布布扣    function S4() {
bubuko.com,布布扣       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
bubuko.com,布布扣    }
bubuko.com,布布扣    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
bubuko.com,布布扣}
bubuko.com,布布扣

 

二、生成GUID的方法二

 
JScript 代码   复制
bubuko.com,布布扣bubuko.com,布布扣
function guid() {
bubuko.com,布布扣    return ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx‘.replace(/[xy]/g, function(c) {
bubuko.com,布布扣        var r = Math.random()*16|0, v = c == ‘x‘ ? r : (r&0x3|0x8);
bubuko.com,布布扣        return v.toString(16);
bubuko.com,布布扣    });
bubuko.com,布布扣}
bubuko.com,布布扣

 

三、生成GUID的方法三

 
JScript 代码   复制
bubuko.com,布布扣bubuko.com,布布扣
function uuid() {
bubuko.com,布布扣    var s = [];
bubuko.com,布布扣    var hexDigits = "0123456789abcdef";
bubuko.com,布布扣    for (var i = 0; i < 36; i++) {
bubuko.com,布布扣        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
bubuko.com,布布扣    }
bubuko.com,布布扣    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
bubuko.com,布布扣    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
bubuko.com,布布扣    s[8] = s[13] = s[18] = s[23] = "-";
bubuko.com,布布扣 
bubuko.com,布布扣    var uuid = s.join("");
bubuko.com,布布扣    return uuid;
bubuko.com,布布扣}
bubuko.com,布布扣

 

四、生成GUID的方法四

 
JScript 代码   复制
bubuko.com,布布扣bubuko.com,布布扣
function uuid(len, radix) {
bubuko.com,布布扣    var chars = ‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz‘.split(‘‘);
bubuko.com,布布扣    var uuid = [], i;
bubuko.com,布布扣    radix = radix || chars.length;
bubuko.com,布布扣 
bubuko.com,布布扣    if (len) {
bubuko.com,布布扣      // Compact form
bubuko.com,布布扣      for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
bubuko.com,布布扣    } else {
bubuko.com,布布扣      // rfc4122, version 4 form
bubuko.com,布布扣      var r;
bubuko.com,布布扣 
bubuko.com,布布扣      // rfc4122 requires these characters
bubuko.com,布布扣      uuid[8] = uuid[13] = uuid[18] = uuid[23] = ‘-‘;
bubuko.com,布布扣      uuid[14] = ‘4‘;
bubuko.com,布布扣 
bubuko.com,布布扣      // Fill in random data.  At i==19 set the high bits of clock sequence as
bubuko.com,布布扣      // per rfc4122, sec. 4.1.5
bubuko.com,布布扣      for (i = 0; i < 36; i++) {
bubuko.com,布布扣        if (!uuid[i]) {
bubuko.com,布布扣          r = 0 | Math.random()*16;
bubuko.com,布布扣          uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
bubuko.com,布布扣        }
bubuko.com,布布扣      }
bubuko.com,布布扣    }
bubuko.com,布布扣 
bubuko.com,布布扣    return uuid.join(‘‘);
bubuko.com,布布扣}
bubuko.com,布布扣

JavaScript生成GUID的方法

标签:style   http   io   os   ar   java   for   strong   sp   

原文地址:http://www.cnblogs.com/ranzige/p/4043800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!