3

Scenario:

For example: Anonymous User enters my website xyz.com, I have to show a popup "Hey welcome", If user opens a new tab without closing the browser and enters xyz.com then we should not show the popup again. If the user closes the browser and come back again to xyz.com then we have to show "Hey welcome".

Note:

No server-side involved. Need a client side solution.

What I tried:

I have tried cookie without expiry

 document.cookie = "welcome=true";

But cookie is not deleted on close of browser.

No, luck with onbeforeunload and unload Since event is triggered on page refresh

Any idea or suggestion will be grateful to solve this issue.

Community
  • 1
  • 1
karthick
  • 5,998
  • 12
  • 52
  • 90
  • 1
    *"I have tried cookie without expiry"* is opposite to what you want to do. Or use `sessionStorage` instead – charlietfl Jun 25 '17 at 17:15
  • I would use the sessionStorage. Here you are: https://www.w3schools.com/html/html5_webstorage.asp – Dawid Karasiewicz Jun 25 '17 at 17:17
  • 1
    @charlietfl you are incorrect. A cookie set without an expiration is considered a browser session cookie. Such cookies should be deleted by the browser upon exit – JAAulde Jun 25 '17 at 17:19
  • data is lost when the browser tab is closed. Please check the scenario If he opens a new tab and enters xyz.com then we should not show the popup again – karthick Jun 25 '17 at 17:19
  • 1
    Possible duplicate of [Chrome doesn't delete session cookies](https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies) – Turnip Jun 25 '17 at 17:23
  • Browser close and session close may not be the same – charlietfl Jun 25 '17 at 17:23
  • Refer this link - https://stackoverflow.com/questions/30370601/delete-cookie-on-browser-close-not-on-page-refresh – Tushar Walzade Jun 25 '17 at 17:25
  • @TusharWalzade I have read the link which you posted But No luck, Actually i have implemented the answer of most upvoted – karthick Jun 25 '17 at 17:28
  • @charlietfl, For ex if i have xyz.com open and gmail, If i close xyz.com, session storage will be deleted and browser remains same. If i open xyz.com in next tab, session wont be there. But actual question is it should work on browser close – gauti Jun 25 '17 at 17:29
  • @Turnip So there is no way to overcome until it gets [resolved](https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies) – karthick Jun 25 '17 at 17:29
  • @karthick I'm not sure it will be "resolved". Chrome, and probably other browsers, need to keep the session cookies in order to restore the page state after a restart. Session cookies have been broken for years. – Turnip Jun 25 '17 at 23:02
  • A large part of the problem is in semantics. There is a difference between a browser session and a web application's session. Since the inception of the HTTP cookie, a cookie without an expiration is considered browser-session cookie. Browser session has always been considered to end at exit of the browser (all windows closed, application quit), regardless of how the web application handles its own sessions. – JAAulde Jun 26 '17 at 15:20
  • @JAAulde I think everyone here is well aware of that. The problem is that browser session cookies _are not_ removed when the browser is closed due to to the "Start where you left off" feature. They no longer behave how they have "Since the inception of the HTTP cookie". – Turnip Jun 26 '17 at 17:56
  • @Turnip I understand. My comment was in reply to some other things said in this thread. I guess that's why SO doesn't recommend long comment threads--context becomes difficult to follow. :) – JAAulde Jun 26 '17 at 18:16

0 Answers0