最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Javascript page refreshunload event - Stack Overflow

matteradmin6PV0评论

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

Post a comment

comment list (0)

  1. No comments so far