记录离开页面时弹出的警告框

log the alert box that pops up when leaving a page

本文关键字:警告 离开 记录      更新时间:2023-09-26

我想记录当我离开一个页面时弹出的警告框(当onbeforeunload, onunload或任何其他事件在离开一个页面时被触发)。所以我重写了alert函数来记录alert函数的调用。然后我设置窗口。位置到其他url以从页面导航。但问题是,当窗口。location被执行,它破坏了我的自定义警报功能,我不能再记录它了。对如何解决这个问题有什么建议吗?编辑我想要记录其警告框的页面是第三方的页面。为了检查它,一个脚本代码被注入到页面的头部,像这样:

<html>
<head>
<script> window.alert = function(str){console.log(‘alert is called: ’ + str);}</script>
</head>
<body onbeforeunload=function(){alert(“you are leaving!”);}>
Sample page
</body>
</html>

当我在这个页面上执行window.location='http://google.com'时,会弹出一个警告框,而不是调用覆盖的警报函数

这可以节省捕捉leave page事件的一些工作:

window.onbeforeunload = function (e) {
  var message = "Your confirmation message goes here.",
  e = e || window.event;
  // For IE and Firefox
  if (e) {
    e.returnValue = message;
  }
  // For Safari
  return message;
};

答案不是我的财产,你可以在这里找到更多:

拦截页面退出事件