/** * This js provide a simple way to operate cookie in javascript * function getCookie(name); * function setCookie(name,value[,expireDays]); * function deleteCookie(name); */ function getCookie(name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) return getCookieVal(j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return ""; } function setCookie(name, value) { var domain = 'finance.ifeng.com'; var path = '/'; var argv = setCookie.arguments; var argc = setCookie.arguments.length; var expDay = (argc > 2) ? argv[2] : -1; try { expDay = parseInt(expDay); } catch(e) { expDay = -1; } if(expDay < 0) { setCookieVal(name, value, null, path, domain); } else { var expDate = new Date(); // The expDate is the date when the cookie should expire, we will keep it for a month expDate.setTime(expDate.getTime() + (expDay * 24 * 60 * 60 * 1000)); //setCookieVal(name, value, expDate, path); setCookieVal(name, value, expDate, path, domain); } } function deleteCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); // This cookie is history var cval = getCookie(name); document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString(); } function getCookieVal(offset) { var endstr = document.cookie.indexOf(";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } function setCookieVal(name, value) { var argv = setCookieVal.arguments; var argc = setCookieVal.arguments.length; var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : null; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; document.cookie = name + "=" + escape(value) + ((expires == null || expires < 0) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : ""); } var setUsersSaw = function(cookiename, code, expDay) { var user_saw_list = getCookie(cookiename); if (code != '') { if (user_saw_list == '') { user_saw_list += code; } else { var temp_arr = code.split(':'); var sign = temp_arr[0]+':'+temp_arr[1]; //代码:名称 var arr = user_saw_list.split(','); var dot_sign = false; if (arr.length < 20) { if (user_saw_list.indexOf(sign) != -1) {//该内容的代码和名称已经存在 user_saw_list = ''; for (var i in arr) { if (arr[i].indexOf(sign) != -1) {//去除 } else { user_saw_list = arr[i] + ','; dot_sign = true; } } if (dot_sign == true) { user_saw_list = user_saw_list.substr(0, user_saw_list.length-1); } } user_saw_list += ',' + code; } else {//当已经浏览过20只时,则删除最早的一只,补充新进的一只 for (i = 1; i < 20; i++) { user_saw_list += code + ',' + arr[i]; } user_saw_list += code; } } } setCookie(cookiename, user_saw_list, expDay); };