Thế Hải Nguyễn Thế Hải Nguyễn - 1 month ago 5
Javascript Question

How to know user reload page by javascript

I stored a variable in sessionStorage. When I reload the webpage, sessionStorage don't set this variable to null. I must close the window and open a new one to do this. So, How can I know user reload page by javascript, I need sessionStorage set to null when I reload page

Answer

As suggested by @sideroxylon

window.addEventListener("beforeunload",function(e){
    sessionStorage.setItem("var",null);
});

This will fire whether the user reloads the page or navigates away.

Comments