1 回答

TA貢獻1820條經驗 獲得超2個贊
我很想將其sessionStorage用于此任務,因為它可以存儲比 cookie 更大的數量,并且您不必擔心在任何階段手動將其拆除,因為它只會保留在會話中。
const hitcounter=function(){
const StoreFactory=function( name, type ){/* this would usually be in a separate js file */
'use strict';
const engine = !type || type.toLowerCase() === 'local' ? localStorage : sessionStorage;
const set=function( data ){
engine.setItem( name, JSON.stringify( data ) );
};
const get=function(){
return exists( name ) ? JSON.parse( engine.getItem( name ) ) : false;
};
const remove=function(){
engine.removeItem( name );
};
const exists=function(){
return engine.getItem( name )==null ? false : true;
};
const create=function(){
if( !exists() ) set( arguments[0] || {} );
};
return Object.freeze({
set,
get,
exists,
create,
remove
});
}
let oStore = new StoreFactory( 'urls', 'sessionStorage' );
oStore.create();
let data=oStore.get();
data[ location.href ]=data.hasOwnProperty( location.href ) ? parseInt( data[ location.href ] ) + 1 : 1;
data.total=data.hasOwnProperty('total') ? parseInt( data.total ) + 1 : 1;
oStore.set( data );
}
document.addEventListener( 'DOMContentLoaded', hitcounter );
顯示存儲數據的表單示例
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>sessionStorage: Maintain list of visited URLS</title>
<style>
span{color:green;display:block;clear:both;}
ul{color:blue}
</style>
<script>
const hitcounter=function(){
const StoreFactory=function( name, type ){/* this would usually be in a separate js file */
'use strict';
const engine = !type || type.toLowerCase() === 'local' ? localStorage : sessionStorage;
const set=function( data ){
engine.setItem( name, JSON.stringify( data ) );
};
const get=function(){
return exists( name ) ? JSON.parse( engine.getItem( name ) ) : false;
};
const remove=function(){
engine.removeItem( name );
};
const exists=function(){
return engine.getItem( name )==null ? false : true;
};
const create=function(){
if( !exists() ) set( arguments[0] || {} );
};
return Object.freeze({
set,
get,
exists,
create,
remove
});
};
const clickhandler=function(e){
oList.innerText = oSpan.innerText = '';
let json=oStore.get();
Object.keys( json ).map( key =>{
let li=document.createElement('li');
li.innerText=key+' '+json[ key ]
if( key!='total' ) oList.appendChild( li )
});
oSpan.innerText='The total is: '+json.total;
};
let oStore = new StoreFactory( 'urls', 'sessionStorage' );
oStore.create();
let data=oStore.get();
data[ location.href ]=data.hasOwnProperty( location.href ) ? parseInt( data[ location.href ] ) + 1 : 1;
data.total=data.hasOwnProperty('total') ? parseInt( data.total ) + 1 : 1;
oStore.set( data );
let oList=document.querySelector( 'form > ul' );
let oSpan=document.querySelector( 'form > span' );
let oBttn=document.querySelector( 'form > input[ type="button"]' );
oBttn.addEventListener('click', clickhandler )
}
document.addEventListener( 'DOMContentLoaded', hitcounter );
</script>
</head>
<body>
<form method='post'>
<ul></ul>
<span></span>
<input type='button' value='Load data' />
</form>
</body>
</html>
添加回答
舉報