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?
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?
-
shouldnt it be
onbeforeunload
– john Smith Commented Jun 28, 2020 at 17:56 - your code isn't guaranteed to run with the unload events. – Daniel A. White Commented Jun 28, 2020 at 17:57
- @johnSmith I dont think so: this and this – kahveciderin Commented Jun 28, 2020 at 17:57
- I'm pretty sure that's no longer possible. – D. Pardal Commented Jun 28, 2020 at 18:07
2 Answers
Reset to default 2In order to call a function before refresh
, you can try the following:
window.addEventListener("beforeunload", function(event) {
myFunction();
});
Try this
window.addEventListener('unload', function(event) {
console.log('hello world');
});