admin管理员组

文章数量:1025208

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?

Share Improve this question asked Jun 28, 2020 at 17:53 kahveciderinkahveciderin 3321 gold badge5 silver badges23 bronze badges 4
  • 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
Add a ment  | 

2 Answers 2

Reset to default 2

In 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');
});

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?

Share Improve this question asked Jun 28, 2020 at 17:53 kahveciderinkahveciderin 3321 gold badge5 silver badges23 bronze badges 4
  • 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
Add a ment  | 

2 Answers 2

Reset to default 2

In 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');
});

本文标签: Javascript page refreshunload eventStack Overflow