I have 5 stages on the signup page so I used the user id to check the user stage by storing his id in local storage. I want to remove the id from localstorage when users leave my website without filling the form. I want to remove local storage after some time.
Is it possible to remove items from local storage when the user is not on my website?
CodePudding user response:
As Estus Flask and Samuel Liew also suggested to use sessionStorage
as per the requirement you have. But still if you want to use localStorage
for any reasons. You can use :
localStorage.removeItem(<your item comes here>)
Called this inside beforeDestroy()
hook as it called right before a vue instance destroy.
This will ensure that before the instance is removed from the DOM, we are removing an item from the localStorage
.
CodePudding user response:
You can use session or cookie storage for that purpose, but if you want to use local storage then you can use onunload or onbeforeunload event listeners of JS and clear local storage on onunload. These listeners work perfectly on tab close or tab back button as well.