在窗口关闭或刷新页面运行JavaScript code? [英] Run JavaScript code on window close or page refresh?

查看:114
本文介绍了在窗口关闭或刷新页面运行JavaScript code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方式来运行,当用户关闭浏览器窗口或刷新页面最后的JavaScript code?

Is there a way to run a final JavaScript code when a user closes a browser window or refreshes the page?

我在考虑类似的东西为onload,但更喜欢的OnClose?谢谢你。

I'm thinking of something similar to onload but more like onclose? Thanks.

我不喜欢onbeforeunload方法,它总是产生一个确认框弹出(休假页/留在Mozilla浏览器)或(重载/不要铬重新加载)。在那里静静地执行code的方法吗?

I don't like the onbeforeunload method, which always yields to a confirmation box popping up (leave page/ stay on mozilla) or (reload/ don't reload on chrome). Is there a way to execute the code quietly?

推荐答案

好吧,我发现这是一个有效的解决方案,它包括使用 beforeunload 事件,然后制作处理程序返回。该执行通缉code没有一个确认框弹起。它是这样的:

Ok, I found a working solution for this, it consists of using the beforeunload event and then making the handler return null. This executes the wanted code without a confirmation box popping-up. It goes something like this:

window.onbeforeunload = closingCode;
function closingCode(){
   // do something...
   return null;
}

希望这有助于。

这篇关于在窗口关闭或刷新页面运行JavaScript code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆