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!


Found a method for a V4 UUID, applied in the following snippet. Test one of the resulting UUIDs here

var result = document.querySelector("#result");
result.textContent = '10 UUIDsn---------------------------------------------n';

for (var i = 0; i < 11; i += 1) {
  result.textContent += UUID() + 'n';
}

function UUID() {
  var uuid = (function() {
    var i,
      c = "89ab",
      u = [];
    for (i = 0; i < 36; i += 1) {
      u[i] = (Math.random() * 16 | 0).toString(16);
    }
    u[8] = u[13] = u[18] = u[23] = "-";
    u[14] = "4";
    u[19] = c.charAt(Math.random() * 4 | 0);
    return u.join("");
  })();
  return {
    toString: function() {
      return uuid.toUpperCase();
    },
    valueOf: function() {
      return uuid.toUpperCase();
    }
  };
}
<pre id="result"></pre>

A simple search on google leads me to this answer, on StackOverflow. First result.

This is actually what @Kooilnc copy-pasted. He didn't paste the second part however:

However, note in the comments that such values are not genuine GUIDs. There's no way to generate real GUIDs in Javascript, because they depend on properties of the local computer that browsers do not expose. You'll need to use OS-specific services like ActiveX: http://p2p.wrox.com/topicindex/20339.htm

PS: I also suggest to look at this library: node-uuid.

链接地址: http://www.djcxy.com/p/16242.html

上一篇: 生成唯一的产品ID

下一篇: 在IE9中使用JavaScript创建GUID?