kahveciderin
kahveciderin

Reputation: 332

Javascript page refresh/unload event

I need to do an animation when the page refreshes/unloads. I tried <body onunload="myFunction()"> but it does not seem to be working. What should I do?

Upvotes: 3

Views: 7783

Answers (2)

Majed Badawi
Majed Badawi

Reputation: 28424

In order to call a function before refresh, you can try the following:

window.addEventListener("beforeunload", function(event) {
     myFunction();
});

Upvotes: 2

sonEtLumiere
sonEtLumiere

Reputation: 4572

Try this

window.addEventListener('unload', function(event) {
  console.log('hello world');
});

Upvotes: 0

Related Questions