Il session storage ci permette di memorizzare qualsiasi dato nella sessione corrente del browser. Questo è anche il caso delle pagine visitate all'interno di un sito.
Si tratta semplicemente di creare un array di URL e salvarlo nel session storage:
var URLStorage = {
init: function() {
this.create();
this.action();
},
create: function() {
// Evito di sovrascrivere i dati già presenti
if( sessionStorage.getItem( "url-storage" ) === null ) {
var obj = {};
obj.items = [];
sessionStorage.setItem( "url-storage", JSON.stringify( obj ) );
}
},
add: function() {
var url = location.href; // URL corrente
var obj = JSON.parse( sessionStorage.getItem( "url-storage" ) );
obj.items.push( url );
sessionStorage.setItem( "url-storage", JSON.stringify( obj ) );
},
action: function() {
// Seleziono i link interni al sito
var self = this,
host = location.host,
links = document.querySelectorAll( "a[href^='http://" + host + "']" ),
len = links.length,
i;
for( i = 0; i < len; ++i ) {
var link = links[i];
// Registro l'URL sul click
link.addEventListener( "click", function( e ) {
e.preventDefault();
var target = this.href;
self.add();
window.location = target;
}, false);
}
}
};
document.addEventListener( "DOMContentLoaded", function() {
URLStorage.init();
});