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 usingonbeforeunload
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.
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
精彩评论