Return Paths From Session

Viewed 96

Using Vanilla Javascript, I need to write a script that console logs an array of every unique page path visited during a browser session.

I am trying to do local storage and set the key with the current pathname. The part that I am getting suck on is pushing another value to that key in local storage.

So far I've gotten

localStorage.setItem('path', window.location.pathname);

But when I re-run that code it overwrites what I had already saved.

Where am I going wrong?

1 Answers

You will need to use JSON stringify/parse for saving and accessing the data.

_saved = localStorage.getItem('path') || "[]";
_saved = JSON.parse(_saved)

_saved.push(window.location.href)

localStorage.setItem('path',JSON.stringify(_saved))

console.log(JSON.parse(localStorage.getItem('path')))
Related