javascriptwindowsinternet-explorer-9

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!


Solution

  • Here's a small method to create a UUID Test one of the resulting UUIDs here

    [edit 2022] See also

    var result = document.querySelector("#result");
    result.textContent = '10 UUIDs\n---------------------------------------------\n';
    
    for (var i = 0; i < 11; i += 1) {
      result.textContent += createUUID() + '\n';
    }
    
    function createUUID() { // RFC 4122-ish
      return Array.from(Array(32))
       .map((e, i) => {
         let someRandomValue = i === 12 ? 4 : (+new Date() + Math.random() * 16) % 16 | 0;
         return `${~[8, 12, 16, 20].indexOf(i) ? "-" : ""}${
           (i === 16 ? someRandomValue & 0x3 | 0x8 : someRandomValue).toString(16)}`;
       }).join("");
    }
    <pre id="result"></pre>