如何在JavaScript中创建GUID(全球独特识别器)?GUID/UUID应该至少有32个字符,并且应该保持在ASCII范围内,以避免在通过它们时遇到麻烦。

我不确定在所有浏览器上有哪些习惯,如何“随机”和种植内置的随机号码发电机等。


当前回答

這裡是一個工作例子. 它生成一個 32 位數的獨特 UUID。

function generateUUID() {
    var d = new Date();
    var k = d.getTime();
    var str = k.toString(16).slice(1)
    var UUID = 'xxxx-xxxx-4xxx-yxxx-xzx'.replace(/[xy]/g, function (c)
    {
        var r = Math.random() * 16 | 0;
        v = c == 'x' ? r : (r & 3 | 8);
        return v.toString(16);
    });

    var newString = UUID.replace(/[z]/, str)
    return newString;
}

var x = generateUUID()
console.log(x, x.length)

其他回答

GitHub上的JavaScript项目 - https://github.com/LiosK/UUID.js

参见 RFC 4122 http://www.ietf.org/rfc/rfc4122.txt. 功能 产生 RFC 4122 符合 UUIDs. 版本 4 UUIDs (UUIDs 从随机数字) 和版本 1 UUIDs (基于时间的 UUIDs) 可用. UUID 对象允许各种访问 UUID 包括访问 UUID 字段。

如果您的环境是 SharePoint,则有一个用途函数称为 SP.Guid.newGuid (MSDN 链接创建一个新的 GUID. 该函数位于 sp.init.js 文件中. 如果您重写此函数(从其他私人函数中删除一些其他依赖),并且它看起来如下:

var newGuid = function () {
    var result = '';
    var hexcodes = "0123456789abcdef".split("");

    for (var index = 0; index < 32; index++) {
        var value = Math.floor(Math.random() * 16);

        switch (index) {
        case 8:
            result += '-';
            break;
        case 12:
            value = 4;
            result += '-';
            break;
        case 16:
            value = value & 3 | 8;
            result += '-';
            break;
        case 20:
            result += '-';
            break;
        }
        result += hexcodes[value];
    }
    return result;
};

我发现这个脚本有用于在JavaScript中创建GUID

HTTPS://github.com/addui/GUIDJS

var myGuid = GUID();

重要的是要使用精心测试的代码,由多个参与者保持,而不是为此擦干自己的东西。

这是一个地方,你可能想偏好最稳定的代码,而不是最短的可能的智能版本,在X浏览器工作,但不考虑到Y的偶像合并,这往往会导致非常硬的调查错误,而不是只随机表现给一些用户。

我想了解布罗法的答案,所以我扩展了它并添加了评论:

var uuid = function () {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
        /[xy]/g,
        function (match) {
            /*
            * Create a random nibble. The two clever bits of this code:
            *
            * - Bitwise operations will truncate floating point numbers
            * - For a bitwise OR of any x, x | 0 = x
            *
            * So:
            *
            * Math.random * 16
            *
            * creates a random floating point number
            * between 0 (inclusive) and 16 (exclusive) and
            *
            * | 0
            *
            * truncates the floating point number into an integer.
            */
            var randomNibble = Math.random() * 16 | 0;

            /*
            * Resolves the variant field. If the variant field (delineated
            * as y in the initial string) is matched, the nibble must
            * match the mask (where x is a do-not-care bit):
            *
            * 10xx
            *
            * This is achieved by performing the following operations in
            * sequence (where x is an intermediate result):
            *
            * - x & 0x3, which is equivalent to x % 3
            * - x | 0x8, which is equivalent to x + 8
            *
            * This results in a nibble between 8 inclusive and 11 exclusive,
            * (or 1000 and 1011 in binary), all of which satisfy the variant
            * field mask above.
            */
            var nibble = (match == 'y') ?
                (randomNibble & 0x3 | 0x8) :
                randomNibble;

            /*
            * Ensure the nibble integer is encoded as base 16 (hexadecimal).
            */
            return nibble.toString(16);
        }
    );
};