如何在浏览器刷新时清除会话存储,但这在单击浏览器后退按钮时不应清除 [英] How to clear the sessionstorage on browser refresh , but this should not clear on click of browser back button

查看:55
本文介绍了如何在浏览器刷新时清除会话存储,但这在单击浏览器后退按钮时不应清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在刷新页面时清除会话存储,而不要在刷新时清除会话存储浏览器的后退按钮单击,或前进单击想要使用angularjs/javascript

I want to clear the session storage only on page refresh but not on the browser back button click , or forward click want to implement this using angularjs/javascript

单击后退按钮时,我正在将数据保存在sessionstorage中,所以我只想单击浏览器刷新按钮时清除相同的数据,但不应该后退

i am saving data in sessionstorage on click of back button so just i want to clear the same data on click of browser refresh button but it should not clear for back

我尝试过

if (performance.navigation.type == 1) {
    $window.sessionStorage.clear();
    // clearing the sessionstorage data
}

// but this one is clearing after clicking 2 times on refresh button

if (performance.navigation.type == 0) {
    $window.sessionStorage.clear();
    // clearing the sessionstorage data

}
// this one is clearing for back button as well ,
// tried onbeforeunload, onpopstate as well its not working its clearing for back button too

// please let me know how to implement this one, thanks in advance
// no jquery please

推荐答案

是的,您可以在Windows加载时实现此目的,然后清除会话存储空间或删除密钥

Yes you can achieve this on windows load and then clear the session storage or removing the key

$window.location.reload();
sessionStorage.clear();

OR

// Remove saved data from sessionStorage
 $window.location.reload();    
 sessionStorage.removeItem('key');

这篇关于如何在浏览器刷新时清除会话存储,但这在单击浏览器后退按钮时不应清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆