Tuesday, June 05, 2007

Generating GUIDs on the client side web browser

If your application requires that a GUID be created to identify the client, then we have 2 options:

- If we are sure that the end users use only IE, then we can use the ActiveX function:
return new ActiveXObject("Scriptlet.TypeLib").
GUID.substr(1,36);
For other browsers, we can write a JS function as shown here.
Basically the function uses the random numbers and padding to generate a unique number.
Snippet of the JS function:
function generateGuid()
{
var result, i, j;
result = '';
for(j=0; j<32; j++)
{
if( j == 8 || j == 12|| j == 16|| j == 20)
result = result + '-';
i = Math.floor(Math.random()*16).
toString(16).toUpperCase();
result = result + i;
}
return result
}