1

I have a piece of code i want fired when the page closes (basically, send a 'disconnected' message to the server. The execution should be fast enough for me not to have to cancel and restart the event. Further to that, it works perfectly in Chrome, Firefox and Safari, but not in IE9 on closing the tab. If i navigate to another page in IE9, my event fires. If i close the tab, it doesn't. I tried the following to bind my code:

jQuery(window).bind("beforeunload", function() { DoSomeWork(); });

i also tried replacing jQuery with $ like so:

$(window).bind("beforeunload", function() { DoSomeWork(); });

Still works in Chrome, but does not work in IE. Any suggestions?

I am using jquery 1.9.1 min (compressed production version).

zaitsman
  • 8,984
  • 6
  • 47
  • 79

1 Answers1

3

Try this

onbeforeunload = function() {
     return "Are you sure";
}

Tested in Chrome Version 26.0.1410.64 m Firefox Version 20.0.1 Internet Explorer Version 9.0

FIDDLE

Khawer Zeshan
  • 9,470
  • 6
  • 40
  • 63
  • Khawer Zeshan, This does work, but IE9 tells me it 'restricted this webpage from running scripts or ActiveX controls' :( – zaitsman May 05 '13 at 00:55
  • You will only see when you are accessing the page directly. If you will access it from web server you will not see it http://stackoverflow.com/questions/7038724/how-to-automaticaly-allow-blocked-content-in-ie – Khawer Zeshan May 05 '13 at 01:09