0

Simple question (probably), but I'm buggered if I can find an answer...

I have a back-end CGI application and a number of dynamically-created webpages (plain ol' HTML+JS+CSS), each with a single form. In every form, in addition to a number of other submit buttons, there is a closeApplication button, which, when clicked, causes the page to be submitted and the CGI application to perform some cleanup processing.

Sometimes however, once thy've done their work, users being what they are (idiots), they don't bother to press the closeApplication button, and simply close the browser window.

I've been trying to figure out how to automatically 'fire' the closeApplication button if the user does this, by using the onUnload handler, but I can't seem to find out how to determine whether the user is closing the browser window/tab or simply submitting the page.

Is there a way within the onUnload handler to find this out?

p.s. I'm just using basic JS - no JQuery or other framework, but I doubt that matters...

roryhewitt
  • 4,097
  • 3
  • 27
  • 33

1 Answers1

1

Unfortunately, you're out of luck.

This question covers very similar ground: how to identify onbeforeunload was caused by clicking close button

What you could possibly do is store a variable when anything valid is clicked on your page that would navigate away, then check that in your onunload handler - if it's false (or whatever), then assume that they've closed the window and submit. I think this would probably be flaky though.

Community
  • 1
  • 1
Town
  • 14,706
  • 3
  • 48
  • 72
  • Yeah, that's what I figured. So, bad for me, but I'll mark yours as the accepted answer. Having said that, your idea about setting a flag actoually WOULD work for me, since I have full control over the onLoad function too, so it would be easy enougn to initialize the flag there and then set if when a submit button is pressed... Not perfect, but it should work. – roryhewitt May 14 '11 at 04:18