1. Knowledge Base
  2. Web SDK questions

How do I end a session when the user refreshes their web page?

Please carefully consider whether this is the behaviour you require, as a consequence of doing this is that sessions will need to be restarted if the user refreshers their browser by mistake or for another reason.

The following code prevents the Cobrowse session continuing after a page refresh:

window.addEventListener("beforeunload", () => {
}, {capture: true});
This does not end the session but it prevents the agent from seeing the user’s screen. You can then end the session on the agent side using any of the available methods.