如何在浏览器刷新时清除会话存储,但这不应该在单击浏览器后退按钮时清除

新手上路,请多包涵

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

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

我努力了

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

原文由 vijay ram 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 338
1 个回答

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

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

或者

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

原文由 md-5h04I3 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题