The onbeforeunload event is often used to warn a user that they have made changes to a web-based form if that user tries to leave the page without saving the changes. This is problematic for pages that use hyperlinks (the <a> anchor tag) to call client-side javascript, even when that code does not result in the user navigating away from the page.
For example, your page may offer a link that allows the user to manipulate content on the page through javascript and the DOM. Normally, you wouldn't want the user to be prompted because of the onbeforeunload event, but this is exactly what happens because you clicked a link and the IE browser believes you may be navigating away from the page.
You can prevent this from happening by using the onclick event of the anchor tag to prevent the onbeforeunload event from firing (set or setting a flag to allow the onbeforeunload event to continue without prompting the user.
Consider the following example where the method of setting a temporary flag is used. The flag is reset after a single use so you would need to call "NoPrompt()" from every link where onbeforeunload processing should be disabled.
<html>
<body>
<a href="javascript:alert('Hello World!');">Warning</a><br/>
<a href="javascript:alert('Hello World!');">No Warning</a>
<script>
// Allow the user to be warned by default.
var allowPrompt = true;
window.>
function WarnUser()
{
if(allowPrompt)
{
event.returnValue = "You have made changes. They will be lost if you continue.";
}
else
{
// Reset the flag to its default value.
allowPrompt = true;
}
}
function NoPrompt()
{
allowPrompt = false;
}
</script>
</body>
</html>