var historycount = 15; //保存历史记录个数 /** * 增加浏览历史记录 * @return */ function sethistory(keyword) { var keywords = $.cookie('keyword'); if (keywords) { if(keyword) { var keys = keywords.split(","); for (var i = keys.length - 1; i >= 0; i--) { if (keys[i] == keyword) { keys.splice(i, 1); } } keys.push(keyword); if (keys.length >= historycount) { //删除最开始的多余记录 var count = keys.length - historycount + 1; //需要删除的个数 keys.splice(0, count); //开始位置,删除个数 } $.cookie('keyword', keys.join(','), {expires: 365, path: '/'}); } } else { $.cookie('keyword', keyword, {expires: 365, path: '/'}); } } function delhistory(){ $.cookie("keyword",null,{path:"/",expires: -1}); } function gethistory(){ var keywords = $.cookie('keyword'); if(keywords) { var keys= keywords.split(","); var length = keys.length; if (length > 0) { $("#historyrecord").empty(); var htmlstring = "<dt>历史搜索</dt><dd>"; for (var i = length - 1; i >= 0; i--) { htmlstring += "<a href='javascript:;' >" + keys[i] + "</a>"; } htmlstring += "</dd>"; $("#historyrecord").html(htmlstring) } } }