js COOKIE 记住帐号或者uuid

时间:2021-01-11 22:57:37

当开始接到这个任务的时候,我对cookie还是没多少了解的,而uuid的生成也是一无所知。但是当你发现这个网址http://*.com/questions/105034/how-to-create-a-guid-uuid-in-javascript,你就会有想法的。呵呵,不仅如此,还有一个网址也不错,推荐下http://www.cnblogs.com/xuzhong718/archive/2012/07/22/2600121.html

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js COOKIE 记住帐号或者uuid</title>
</head>
<body>
<script type="text/javascript">
function guidGenerator() {
var S4 = function() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
} var cookie = {};
cookie.set = function(n, v, t) {
var exp = new Date();
exp.setTime(exp.getTime() + (t || 24) * 60 * 60 * 1000 * 365);
document.cookie = n + "=" + escape(v) + ";expires=" + exp.toGMTString() + ';path=/';
} cookie.get = function(n) {
var arr = document.cookie.match(new RegExp("(^| )" + n + "=([^;]*)(;|$)"));
if (arr != null) {
return unescape(arr[2]);
}
return null;
} var uniqueVisitorId;
var co = cookie.get('uniqueVisitorId');
if (co != null) {
uniqueVisitorId = cookie.get('uniqueVisitorId');
} else {
uniqueVisitorId = guidGenerator();
cookie.set('uniqueVisitorId', uniqueVisitorId);
}
var url = window.location.href;
var monitorUrl = 'http://130.251.101.3:18080/monitor/add?url=' + url + '&uniqueVisitorId=' + uniqueVisitorId;
var code = "<" + "script src='" + monitorUrl +"'></"+"script>";
document.write(code); </script>
</body>
</html>