Generate a GUID in javascript

I needed to do this to create unique id’s for polygons in Virtual Earth, that would also be unique on the server as well as the client. This format is valid for passing to as a guid parameter of a web service.
 

function NewGuid()
{
   var guid = ""
;
   for (var
i = 0; i < 32; i++)
      guid += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 7 || i == 11 || i == 15 || i == 19 ?
"-" : ""
)
   return
guid;
}

Advertisements