Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17566

how to call a function before page reload jquery

I have a few links in my page and I want to call a function when the page is trying to reload.

I tried

$(window).unload(function() {
  alert('Handler for .unload() called.');
});

It is not working.

Upvotes: 0

Views: 12224

Answers (3)

Rethina Mahesh
Rethina Mahesh

Reputation: 1

window.onbeforeunload = unloadMessage;

   function unloadMessage() {
           //do your codeing here...
   }

Upvotes: 0

c24w
c24w

Reputation: 7856

Console: Blocked alert('Handler for .unload() called.') during unload. (in Chrome)

I assume this is blocked for user-experience reasons.

If you try console.log('Handler for .unload() called.'); the call is made successfully.

Edit: see $(window).unload is not firing

Upvotes: 4

thecodeparadox
thecodeparadox

Reputation: 87073

you can try:

window.onbeforeunload = function() {

}

Upvotes: 4

Related Questions