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

问题描述 投票:0回答:1

我想记录当我离开页面时弹出的警告框(当离开页面时onbeforeunload,onunload或任何其他事件被触发)。所以我覆盖了警报功能来记录警报功能调用。然后我将window.location设置为其他URL以远离页面。但问题是,当window.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'时,会弹出一个警告框,而不是调用该覆盖的警报功能。

javascript alert onbeforeunload
1个回答
0
投票

这可以节省一些捕获休假页面事件的工作:

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

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

Intercept page exit event

© www.soinside.com 2019 - 2024. All rights reserved.