views:

407

answers:

2

I'm implementing a search service called SearchInsideOut. http://bit.ly/97SFyW
This search service simply replaces web page 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.

A: 

See my comment here on how window.onbeforeunload works: Hopefully this will help you determine whether you want to take the window.onbeforeunload approach or write a better script on it.

The Elite Gentleman
A: 

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

Adrian