开发者

onbeforeunload dilemma: iframe breaking vs. annoying message on refresh/back buttons click

I'm implementing a search service called SearchInsideOut. This search service simply replaces web pag开发者_如何学Pythone results by full web pages (Yes, I used iframe).

The problem I have to deal with is iframe-breaking pages.

The promising solution I found is using onbeforeunload to let users decide whether to stay or leave my site.

But this also creates another annoying behavior. When users click other links in my site, onbeforeunload will also be triggered.

Fortunately, I could solve this case by placing window.onbeforeunload=null in the onclick event of those links of my site.

Unfortunately, I have no idea how to detect external events like clicking "refresh/back" buttons.

What should I do to solve this difficulty?

All suggestions and comments are highly appreciated.


Take as example this site. Try to refresh the page after u typed some text answering to a post. You will see that the page it will ask you to continue ur work or exit. Make another test without answering to a post, just refresh, you will see that the confirm will not showed, because the site is making a test if the "answer" is empty or not. Example:

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

So it's up to u to decide when the box will show or not. Visit also Microsoft Msdn to understand when and how onbeforeunload works

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜