How to clear the sessionstorage on browser refresh , but this should not clear on click of browser back button
我只想在页面刷新时清除会话存储,而不能在
浏览器的后退按钮单击,或前进单击
想要使用angularjs / javascript实现
我要在单击后退按钮时将数据保存在sessionstorage中,所以我只想在单击浏览器刷新按钮时清除相同的数据,但不应该清除后退
我试过了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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加载时实现此目的,然后清除会话存储或删除密钥
1 2 | $window.location.reload(); sessionStorage.clear(); |
OR
1 2 3 | // Remove saved data from sessionStorage $window.location.reload(); sessionStorage.removeItem('key'); |
您保留设置sessionStorage的页面地址,然后在onload事件上对其进行检查。 如果它们相同,则擦除sessionStorage内容。
1 2 3 | window.onbeforeunload = function(){ sessionStorage.setItem("origin", window.location.href); } |
然后在onload事件中:
1 2 3 4 5 | window.onload = function(){ if(window.location.href == sessionStorage.getItem("origin")){ sessionStorage.clear(); } } |
尝试使用
1 2 3 4 5 | $rootScope.$on('$stateChangeStart', function(event, toState, fromState){ if(toState.name === fromState.name){ $window.sessionStorage.clear(); } }) |
希望这会有所帮助!