kaze kaze - 6 months ago 34
Javascript Question

Create GUID with JavaScript in IE9?


Possible Duplicate:

How to create a GUID / UUID in Javascript?

JavaScript: how to generate UUID for Internet Explorer 9?




Is there a way, completely windows platform dependent, of creating a GUID in javascript for IE9. It does not have to support other browsers or OSs.

Thanks!

Answer
function guid(){
 function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
 }
 return [S4()+S4(),'-',S4(),'-',S4(),
         '-',S4(),'-',S4()+S4()+S4()].join('').toUpperCase();
}

Alternatively, I found this code to produce a V4 UUID:

function UUID() {
    var uuid = (function () {
        var i,
            c = "89ab",
            u = [];
        for (i = 0; i < 36; i += 1) {
            u[i] = (Math.random() * 16 | 0).toString(16);
        }
        u[8] = u[13] = u[18] = u[23] = "-";
        u[14] = "4";
        u[19] = c.charAt(Math.random() * 4 | 0);
        return u.join("");
    })();
    return {
        toString: function () {
            return uuid.toUpperCase();
        },
        valueOf: function () {
            return uuid.toUpperCase();
        }
    };
}
Comments