Reputation: 4886
Is it possible to somehow clear the localStorage value when navigationg away from the current page.
I am trying to save the scrollTop value of the page on a localStorage variable and use it to scroll to that point incase the page is refreshed .
But If I go to Different page I want the localStorage value to be set ot null again .
Is there any easy way I can do it
UPDATE
Added the answer provied by Blaze
to my script and its generating error
Script
$(document).on('vclick','#outerPage',function(e){
// alert('yo i am clickable now');
var parentOffset = $(this).parent().offset();
//or $(this).offset(); if you really just want the current element's offset
var relX = e.pageX - parentOffset.left;
var relY = e.pageY - parentOffset.top;
// localStorage.setItem("scrollPosition", relY);
//storePosition.topCoordinate = relY;
//alert(relY);
obj = JSON.parse( localStorage.getItem('scrolls') );
obj[location.pathname] = relY;
localStorage.setItem('scrolls', JSON.stringify(obj) );
console.log(localStorage.getItem("scrolls"));
});
ERROR
Uncaught TypeError: Cannot set property '/url.html' of null
Thanks & Regards
Upvotes: 1
Views: 1086
Reputation: 3934
Yes
just use the unload function with jquery jquery/unload
On the unload function, just clear the localstorage
localStorage.clear();
Upvotes: 1