最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript page refreshunload event - Stack Overflow

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论