2

So, here I want just to alert true, when window is closed ( I mean particullary tab in browser).

$(document).ready(function(){



});
$(window).unload(function(){
    alert('true'); });

tried $(window) also inside $(document).ready(), nothing.

Joe Half Face
  • 2,303
  • 1
  • 17
  • 45
  • what browser are you using? I think Chrome may prevent stuff like this from happening (because it's annoying) – Codeman May 13 '13 at 22:47
  • Firefox, but actually I don't want to attack user with alert, just delete cookie, here I'm just testing – Joe Half Face May 13 '13 at 22:48
  • you might try `onbeforeunload`...http://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-ch – MikeM May 13 '13 at 22:49

1 Answers1

5

You're very limited in what you can do in the context of window.unload. The browsers won't let you do anything to force the user to stay on the page, and that includes calling alert.

The best thing you're allowed to do is return a string from an onbeforeunload handler - the browser will display that to the user, along with a question like "Are you sure you want to leave this page?"

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • 1
    @JoeHalfFace doesn't matter. It's only code used to bind the callback. It's not executed until the event is fired. – Reactgular May 13 '13 at 22:50
  • thanks, I will accept when SoF lets me, it is so stupid, I wanted to delete cookie, but to test if event works decided to try it with alert, and spent nearly hour trying out to figure out why there is no alert – Joe Half Face May 13 '13 at 22:53