生成UUID、GUID
javascript
1.通过随机数生成(兼容IE)
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
2.高版本浏览器可以使用如下方法(IE不支持),在console中查看crypto存不存在就知道支持情况了
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = crypto.getRandomValues(new Uint8Array(1))[0]%16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
node.js
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = crypto.randomBytes(1)[0]%16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
java
/**
* 创建UUID
* @return
*/
public static String creatUUID(){
return java.util.UUID.randomUUID().toString().replace("-", "");
}