I have the following Jquery function to notify the user with a prompt when they have unsaved changes (similar to how SO does it) and are trying to leave the current screen.
<!-- in my aspx page -->
<script type="text/javascript" language="javascript">
window.onbeforeunload = function()
{
if (HasPendingUpdates())
{
return "Changes you have made will not be saved.";
}
}
</script>
This works as expected and shows the message box when the user tries to click a href to browse away or use the back button etc. The problem I am having is that one of my links is a asp:LinkButton
because it needs to fire off some server side code before leaving the page. When the user clicks this LinkButton
they get the prompt twice.
Scenarios:
- User clicks the
LinkButton
- Prompt appears and user clicks the cancel button.
Prompt disappears and user is still on screen. GOOD.
User clicks the
LinkButton
- Prompt appears and clicks the OK button.
- Prompt disappears and the same prompt shows again.
- User clicks OK again.
- Prompt disappears and user moves to the next screen and all is good.
So why am I getting the second prompt??? How is the OnBeforeUnload firing twice?