Create Globally Unique ID (GUID) in JavaScript

In this blog post I will explain how to create globally-unique identifiers in JavaScript.

This will be a version 4 compliant solution that solves the chance for collision by offsetting the first 13 hex numbers by a hex portion of the timestamp. That way, even if Math.random is on the same seed, both clients would have to generate the GUID at the exact same millisecond (or 10,000+ years later) to get the same GUID:

function generateGuid(){
    var d = new Date().getTime();
    if(window.performance && typeof window.performance.now === "function"){
        d += performance.now();; //use high-precision timer if available
    }
    var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    });
    return guid;
}

Hope it may be useful!