I'm trying to put up a warning message about leaving a webpage, but it is nested inside a frameset.
Apparently window.onbeforeunload triggers if I load the webpage as the root page in the browser, but doesn't trigger when loaded inside the frameset.
Edit: It doesn't work in Chrome 7 or Safari 5, but it works in Internet Explorer 8 and FireFox 3.6. Is there a fix/workaround so that I can get it working in Chrome 7 and/or Safari 5 as well?
What am I doing wrong?
This is what I tried:
function closePageWarning()
{
return "Are you sure you want to leave this page?"
}
window.onbeforeunload = closePageWarning
Test documents:
<html>
<body>
<script language="javascript">
function closePageWarning()
{
return "Are you sure you want to leave this page?";
}
window.onbeforeunload = closePageWarning;
</script>
<a href="http://www.bitbucket.org">bitbucket</a>
</body>
</html>
<html>
<frameset cols="20%, 80%">
<frame name="menu" src="test3.html" />
<frame name="content" src="test1.html" />
</frameset>
</html>
<html>
<body>
<a target="content" href="http://www.bitbucket.org">bitbucket</a>
</body>
</html>
How to test:
Tested in:
In the context of a frame, window
refers to the frame, which is a kind of window. So window.onbeforeunload
refers to an event of the window in the frame, which is not fired when the root page is unloaded. You can refer to the root window by window.top
, or to the direct parent window of the frame by window.parent
. However, these are not standard properties, so use them at your own peril!