可以在页面重新加载时触发 JavaScript 功能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8692603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:14:29 来源:igfitidea点击:
Possible to trigger JavaScript function on page reload?
提问by Justin John
Is it possible to trigger a JavaScript function when a user tries to reload the current page?
当用户尝试重新加载当前页面时,是否可以触发 JavaScript 函数?
采纳答案by Joseph
you mean confirm an "unload" whereby execute a function before the user leaves the current page.
您的意思是确认“卸载”,从而在用户离开当前页面之前执行一个功能。
<html>
<head>
<script type="text/javascript">
function mymessage()
{
//this is the sample function code executed on "unload"
alert("This message was triggered from the onunload event")
}
</script>
</head>
<body onunload="mymessage()">
<p>close this window</p>
</body>
</html>
OR
或者
the jQuery version http://api.jquery.com/unload/
jQuery 版本http://api.jquery.com/unload/