使用javascript处理刷新页面事件

使用javascript处理刷新页面事件,javascript,javascript-events,Javascript,Javascript Events,是否可以使用javascript来处理刷新页面的事件? 如果用户有以下行为之一,我希望得到通知: 按F5刷新页面 关闭选项卡或浏览器 输入一个新的url,然后按enter键 浏览者 是否显示警告消息? 提前谢谢 你能得到的最接近的事件是: 请务必注意,您需要从该函数返回一个字符串。如果不需要刷新,则需要onbeforeunload事件 文章中的示例代码 <HTML> <head> <script> function closeIt() { retur

是否可以使用javascript来处理刷新页面的事件? 如果用户有以下行为之一,我希望得到通知:

  • 按F5刷新页面
  • 关闭选项卡或浏览器
  • 输入一个新的url,然后按enter键 浏览者
是否显示警告消息?

提前谢谢

你能得到的最接近的事件是:


请务必注意,您需要从该函数返回一个字符串。

如果不需要刷新,则需要onbeforeunload事件

文章中的示例代码

<HTML>
<head>
<script>
function closeIt()
{
  return "Any string value here forces a dialog box to \n" + 
         "appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
  <a href="http://www.microsoft.com">Click here to navigate to 
      www.microsoft.com</a>
</body>
</html>

函数closeIt()
{
return“此处的任何字符串值都会强制对话框\n”+
“在关闭窗口之前显示。”;
}
window.onbeforeunload=关闭它;

我非常确定,对于IE/FF,您不需要使用
e.returnValue
执行任何操作。您只需要返回一个字符串。@MattBall-它指的是IE<8和Firefox版本4之前的版本。请参阅。正在使用最新版本的chrome:)不要使用chrome的当前版本
60.0.3112.101(官方版本)(64位)
<HTML>
<head>
<script>
function closeIt()
{
  return "Any string value here forces a dialog box to \n" + 
         "appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
  <a href="http://www.microsoft.com">Click here to navigate to 
      www.microsoft.com</a>
</body>
</html>