1

What I'm trying to do is to flush localStorage when user move to other page.

For example, let say I'm currently in http://foo.com/accounts/mypage.

When user moves to url, http://foo.com/album, I want to flush localStorage.

This is my JQuery script.

$(window).unload(function(){
    if ((window.location.pathname).indexOf('mypage') < 0) {
        localStorage.flush();
    }
});

But it doesn't work......

Any other nice idea?

user3595632
  • 5,380
  • 10
  • 55
  • 111
  • 3
    Possible duplicate of [Clear localStorage](http://stackoverflow.com/questions/7667958/clear-localstorage) – Ish Sep 02 '16 at 09:07

2 Answers2

0
localStorage.clear();

Please try this

Ish
  • 2,085
  • 3
  • 21
  • 38
0

Add a click handler to your links, like this:

var lastLink = "";
$("body").on("click", "a", function() {
    lastLink = $(this).attr("href");
});

This click will run before the unload event and you will be able to use lastLink as your value. Just make sure you take into account relative urls as well.

//TODO: implement function isMine
$(window).unload(function() {
    if (isMine(lastLink)) {
        localStorage.clear();
    }
});
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175