[CLOSED] Solution How to detect browser/tab close



here my working solution for detect when user close browser or a broser tab.

The real trick is to add jquery before load wisej in default.html

in program.vb there is the method BrowserClosed that is called after the closing. Here you can put you code.

in AssemblyInfo.vb uncomment <Assembly: Wisej.Core.WisejResources(“”)> for embedd javascript or put it in initscript.

if you don’t want a message for exit comment this in javascript

window.addEventListener(“beforeunload”, function (e) {
var confirmationMessage = “Exit message”;
e.returnValue = confirmationMessage;     // Gecko and Trident
return confirmationMessage;              // Gecko and WebKit

I hope it is useful for someone, I attach an example project


Cristian Zerbinati

  • You must to post comments

Hi Luca,

mmmhhh  right I have used Firefox but Chrome detect refresh.

well at least I can immediately know when the browser or tab is closed 🙂

  • You must to post comments

Hi Christian,

Thank you but… 🙂

  • You can enable the unload confirmation using Application.EnableUnloadConfirmation = True
  • Chrome sometimes completely ignores it and doesn’t show any alert.
  • You cannot display a message anymore, only old IE shows the string.
  • “beforeunload” and “unload” are also fired when refreshing the page (F5 or change the URL with a parameter)
  • Using ajax in “unload” doesn’t work, the browser kills the communication. It may appear to work at times, but it doesn’t because the unload event terminates all ajax requests and you can’t block it. You could use async ajax, but it’s deprecated. Same issue in “beforeunload”.
  • Also, you don’t need jQuery for your test, you can simply put the code in LeavePageQuestion() outside of the function.

Basically it is unfortunately established that it is impossible to detect when the user closes a tab or the browser versus when the user hits refresh or simply navigates or changes the parameters. All browser makers (basically chrome and firefox) have been clear that they do not allow that distinction.





  • You must to post comments
Showing 2 results