Generating Unique Strings in Javascript

Here is a simple way to generate a unique string in javascript.

    var ts = (new Date()).getTime().toString();
    var url = "ajaxHandler.php?param1=value1&paramn=valuen&t=" + ts;
    //do ajax call...

This method is very helpful for ajax developers, to overcome the problem of cached ajax requests in IE.

Update:
Here’s another way of creating unique strings

String.unique = String.guid = String.uid = String.uuid = function(){
    var idx = [], itoh = '0123456789ABCDEF'.split('');

    // Array of digits in UUID (32 digits + 4 dashes)
    for (var i = 0; i < 36; i++) { idx[i] = 0xf & Math.random() * 0x10; }
    // Conform to RFC 4122, section 4.4
    idx[14] = 4; // version
    idx[19] = (idx[19] & 0x3) | 0x8; // high bits of clock sequence

    // Convert to hex chars
    for (var i = 0; i < 36; i++) { idx[i] = itoh[idx[i]]; }

    // Insert dashes
    idx[8] = idx[13] = idx[18] = idx[23] = '-';

    return idx.join('');
}

Sample Use:

   var id=String.guid();
   va u = String.uuid();
Advertisements