Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Detect tab/browser closed

Hi everyone,

I want to delete user data when user close the browser tab or window. I'm kind of able to achieve the same with beforeunload event but wasn't able to differentiate between reload or close event.

1 Answer

I found this comment on Stack Overflow:

"I just tried this and it solved the issue: Create a sessionStorage object which will get destroyed when the user closes the browser. We can check the sessionStorage object to find if the user has closed the browser or refreshed the page(sessionStorage object will not be destroyed on page refresh)."

More info:

https://webkul.com/blog/detect-browser-tab-refresh-browser-tab-close-using-javascript/

I hope that helps. Happy coding!