浏览历史记录

时间:2022-07-30 13:12:57
  <script src="Scripts/json2.js" type="text/javascript"></script>
<script type="text/javascript">
var art_title = $("title").text(); //文章标题
var art_url = document.URL; //页面地址
$(function () {
//加载浏览历史记录
GetBrowserHistoryFromCookie();
});
//历史浏览记录
function GetBrowserHistoryFromCookie() {
var art_title = $("title").text(); //文章标题
var art_url = document.URL; //页面地址
if ($.cookie("HistoryRecord") == null) {
var json = { "root": [{ "title": "" + art_title + "", "url": "" + art_url + ""}] };
//将json对象转换为字符串
var text = JSON.stringify(json);
$.cookie("HistoryRecord", text);
} else {
//将字符串转换为Json
var tempData = JSON.parse($.cookie("HistoryRecord"));
if (tempData["root"].length >= 10) {
tempData["root"].shift()
}
tempData["root"].push({ "title":"" +art_title+"", "url": ""+art_url+"" });
$.cookie("HistoryRecord", JSON.stringify(tempData));
}
$(".history ul").empty();
for (var i =9; i >= 0; i--) {
$(".history ul").append(" <li><a href=\"" + JSON.parse($.cookie("HistoryRecord"))["root"][i]["url"] + "\">" + JSON.parse($.cookie("HistoryRecord"))["root"][i]["title"] + "</a></li>");
}
}
</script>