1、主页:https://sitename.com/ 的javascript代码是:
if(window.localStorage){
var sitename_his=JSON.parse(localStorage.getItem('sitename_his'));
if(sitename_his==null || typeof sitename_his !='object'){sitename_his=[];
document.getElementById("medical-recommend-list").innerHTML='无记录';
}else{
var thtml='';
for (var i = 0; i < sitename_his.length; i++) {
thtml +='<a href="'+sitename_his['u']+'" class="bb mri2"><div class="s16 mrt3">'+sitename_his['t']+'</div></a>';
if(i>5) break;
}
document.getElementById("medical-recommend-list").innerHTML =thtml;
}
}
2、查看历史记录:https://sitename.com/his.html 的javascript代码是:
if(window.localStorage){
var sitename_his=JSON.parse(localStorage.getItem('sitename_his'));
if(sitename_his==null || typeof sitename_his !='object'){sitename_his=[];
document.getElementById("medical-recommend-list").innerHTML='无记录';
}else{
var thtml='';
for (var i = 0; i < sitename_his.length; i++) {
thtml +='<a href="'+sitename_his['u']+'" class="bb mri2"><div class="s16 mrt3">'+sitename_his['t']+'</div></a>';
}
document.getElementById("medical-recommend-list").innerHTML =thtml;
}
}
3、查看收藏:https://sitename.com/fav.html 的javascript代码是:
if(window.localStorage){
var sitename_fav=JSON.parse(localStorage.getItem('sitename_fav'));
if(sitename_fav==null || typeof sitename_fav !='object'){sitename_fav=[];
document.getElementById("medical-recommend-list").innerHTML='无记录';
}else{
var thtml='';
for (var i = 0; i < sitename_fav.length; i++) {
thtml +='<a href="'+sitename_fav['u']+'" class="bb mri2"><div class="s16 mrt3">'+sitename_fav['t']+'</div></a>';
}
document.getElementById("medical-recommend-list").innerHTML =thtml;
}
}
4、文章浏览:https://sitename.com/i/28/54999.html 的javascript代码是:
var nurl=window.location.href.split('?')[0];
var ntitle=document.title.split(' - ')[0];
if(window.localStorage){
var sitename_his=JSON.parse(localStorage.getItem('sitename_his'));
if(sitename_his==null || typeof sitename_his !='object') sitename_his=[];
sitename_his.unshift({'u':nurl,'t':ntitle});
for (var i = 1; i < sitename_his.length; i++) {
if(sitename_his['u']==nurl){
sitename_his.splice(i, 1);
break;
}
}
if(sitename_his.length>100) sitename_his.pop();
localStorage.setItem('sitename_his',JSON.stringify(sitename_his));
/*收藏*/
}
var fav_bool=false;
function is_fav(){
if(window.localStorage){
var sitename_fav=JSON.parse(localStorage.getItem('sitename_fav'));
if(sitename_fav==null || typeof sitename_fav !='object'){document.getElementById("fav").innerText='加入收藏'; return false}
for (var i = 0; i < sitename_fav.length; i++) {
if(sitename_fav['u']==nurl){
document.getElementById("fav").innerText='取消收藏'
fav_bool=true;
break;
}
}
document.getElementById("fav").innerText='加入收藏'
}
}
function add_fav(){
if(window.localStorage){
var sitename_fav=JSON.parse(localStorage.getItem('sitename_fav'));
if(sitename_fav==null || typeof sitename_fav !='object') sitename_fav=[];
if(!fav_bool){sitename_fav.unshift({'u':nurl,'t':ntitle});
fav_bool=true;
var stat=1;
document.getElementById("fav").innerText='取消收藏'
}else{
var stat=0;
fav_bool=false;
document.getElementById("fav").innerText='加入收藏'
}
for (var i = stat; i < sitename_fav.length; i++) {
if(sitename_fav['u']==nurl){
sitename_fav.splice(i, 1);
break;
}
}
if(sitename_fav.length>100) sitename_fav.pop();
localStorage.setItem('sitename_fav',JSON.stringify(sitename_fav));
}
}
is_fav();
总结:4步,简简单单就实现了本地的浏览记录缓存和收藏夹功能,大家感觉去试试吧