如何仅为当前页面使用本地存储。在Jquery

时间:2014-09-25 13:03:43

标签: jquery html5 jquery-mobile local-storage

当导航离开当前页面时,是否有可能以某种方式清除localStorage值。

我正在尝试在localStorage变量上保存页面的scrollTop值,并使用它滚动到该点,以便刷新页面。

但是如果我转到不同页面,我希望localStorage值再次设置为null。

有什么简单的方法可以做到吗

更新

Blaze提供的答案添加到我的脚本及其生成错误

脚本

$(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 

谢谢&问候

1 个答案:

答案 0 :(得分:1)

只需使用jquery jquery/unload

的卸载功能

在卸载功能上,只需清除localstorage

即可
localStorage.clear();