1

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

3
  • One thing I can think of is to use localStorage. Set some value to it initially.. if the user reloads the page you can check this value and open a new tab. Commented Oct 22, 2016 at 1:56
  • you want to know how if the users reloads by javascript, but that is a very odd use case, typically users do not use javascript to reload the page, but use the reload button or something similar Commented Oct 22, 2016 at 1:57
  • Have you tried onbeforeunload? Commented Oct 22, 2016 at 1:57

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Tks u very much :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.