刷新页面时localStorage变空

时间:2016-02-12 04:53:56

标签: javascript

当我刷新页面时,

localStorage变空。

我在代码中所做的是: - 1)我已将authToken存储在localStorage中 2)我在sessionStorage中存储了isReloaded值,以检查页面是否是新打开的还是回发

如果首次打开浏览器选项卡,则在tabCount localStorage中增加浏览器选项卡计数值,如果重新加载浏览器,则不应在tabCount中添加任何值,如果我关闭所有选项卡而不注销页面,则所有localStorage都应该清除。

实现代码的目的: -

1)页面应清除localStorage值,如果所有浏览器选项卡都关闭而不注销页面。 2)页面实例应该在多个选项卡中打开

$(window).load(function () {


if(!sessionStorage.getItem('isReloaded'))
{
    //first time call

   if(localStorage.getItem('tabCount') == null || localStorage.getItem('tabCount') == undefined)
   {
       var tabCount = 1;
       localStorage.setItem('tabCount',tabCount.toString());    
   }
   else
   {
      var tabCount = parseInt(localStorage.getItem('tabCount'));
      tabCount += 1;
      localStorage.setItem('tabCount',tabCount.toString()); 
   } 


}
else
{
    // postback code here
    console.log('in postback...')
}

 sessionStorage.setItem('isReloaded',true);

});

$(window).unload(function(){

     var tabCount = parseInt(localStorage.getItem('tabCount'));
     if(tabCount > 0)
     {
        tabCount -= 1;
        if(tabCount == 0)
        {
            //RemoveLocalStorage('tabCount,authToken,userId,userName,userFullName,caseNo,consumerName, consumerFirstName, consumerLastName, consumerList, userData,timeStamp');
           localStorage.setItem('tabCount',tabCount.toString());                   
           localStorage.clear();
        }
        else
        {
          localStorage.setItem('tabCount',tabCount.toString()); 
        }                   
     }

});

0 个答案:

没有答案