onbeforeunload困境:iframe打破与刷新/后退按钮上的恼人消息点击

时间:2010-03-10 08:06:16

标签: iframe refresh onbeforeunload back-button

我正在实施名为SearchInsideOut的搜索服务。 此搜索服务只是用完整的网页替换网页结果(是的,我使用iframe)。

我必须处理的问题是iframe破解页面 我找到的有希望的解决方案是使用onbeforeunload让用户决定是留下还是离开我的网站 但这也会造成另一种恼人的行为。 当用户点击我网站中的其他链接时,也会触发onbeforeunload

幸运的是,我可以通过将window.onbeforeunload=null放入我网站链接的onclick事件来解决此问题。
不幸的是,我不知道如何检测外部事件,例如点击“刷新/返回”按钮 我该怎么做才能解决这个难题?

非常感谢所有建议和评论。

1 个答案:

答案 0 :(得分:0)

以此网站为例。在输入一些回复帖子的文本后尝试刷新页面。您将看到它将要求您继续工作或退出的页面。在没有回复帖子的情况下进行另一次测试,只需刷新,您将看到确认不会显示,因为如果“回答”为空,该网站正在进行测试。 例如:

<html>
<head>
<script>
function closeIt()
{
  var mytext = document.getElementById("mytext").value;
  if(mytext != "")
     return "Any string value here forces a dialog box to \n" + 
         "appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
  <textarea id="mytext"></textarea>
</body>
</html>

所以由你来决定盒子何时显示。另请访问Microsoft Msdn了解onbeforeunload的工作时间和方式