I have some check boxes in one of my webpages, and this page has to refresh every 1 minute for some data consistency issues.
I am using window.sessionStoage
to persist the check boxes that have been checked so that the user doesn't lose the boxes already checked on page refresh.
But I want to clear the sessionStorage
when the user navigates away from that page (not necessarily leaving my website, might be going to another page on the same website), and for the same if I use the onunload
event then the storage will be cleared in case of refresh also.
Is there any other event or any workaround that could help me achieve this.
I have some check boxes in one of my webpages, and this page has to refresh every 1 minute for some data consistency issues.
I am using window.sessionStoage
to persist the check boxes that have been checked so that the user doesn't lose the boxes already checked on page refresh.
But I want to clear the sessionStorage
when the user navigates away from that page (not necessarily leaving my website, might be going to another page on the same website), and for the same if I use the onunload
event then the storage will be cleared in case of refresh also.
Is there any other event or any workaround that could help me achieve this.
Share Improve this question edited Dec 29, 2016 at 14:33 Ashish Bahl 1,5021 gold badge18 silver badges28 bronze badges asked Dec 29, 2016 at 12:34 Shubham BatraShubham Batra 1,2781 gold badge14 silver badges27 bronze badges 1- 1 You could use session or local storage to set a variable at the top of the page, to determine whether or not this page is the same page. And if it is or is not, clear the session. – Andrew Ice Commented Dec 29, 2016 at 14:38
2 Answers
Reset to default 2May you try to save the path/name of the current page in you session-storage and on each init of your page check if the pagename equals the name in the sessionstorage. If not, you can clear it.
For your particular case window events can be in handy. So you can react to the blur and focus events, so leaving your browser tab will cause blur
event.
$(window).blur(function(){
//clear session
});
$(window).focus(function(){
//maybe some code to re-init session data
});