我只想在页面刷新时清除会话存储,而不是在
单击浏览器后退按钮,或单击前进
想要使用 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
您保留设置 sessionStorage 的页面地址,然后在 onload 事件上检查它。如果相同,则删除sessionStorage内容。
window.onbeforeunload = function() {
sessionStorage.setItem("origin", window.location.href);
}
然后在onload事件中:
window.onload = function() {
if (window.location.href == sessionStorage.getItem("origin")) {
sessionStorage.clear();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)