|
var initialValue = ''; var userValue = ''; var NAVIGATE_AWAY_MESSAGE = "The changes you made will be lost if you navigate away from this page."; var onBeforeUnloadFired = false; var DISABLE_ONBEFOREUNLOAD_PLUGIN = false; var IgnoreNavigateForEventSource = false; $(document).ready(function() { initialValue = GetFormValues(); window.onbeforeunload = handleOnBeforeUnload; $("a,input,img").click(function() { IgnoreNavigateForEventSource = $(this).hasClass("nonavigate") }) }); function DisableNavigateAway() { DISABLE_ONBEFOREUNLOAD_PLUGIN = true } function SetNavigateAwayMessage(message) { NAVIGATE_AWAY_MESSAGE = message } function IgnoreNavigateAwayFor(elementId) { $("#" + elementId).addClass('nonavigate') } function GetFormValues() { var formValues = ''; $.each($('form').serializeArray(), function(i, field) { if (field.name != '__EVENTVALIDATION' && field.name != '__EVENTTARGET' && field.name != '__EVENTARGUMENT' && field.name != '__VIEWSTATE' && field.name != '__VIEWSTATEENCRYPTED') { var inputField = $("[name=" + field.name + "]"); var displayProperty = $(inputField).css("display"); if (displayProperty != "none") { formValues = formValues + "-" + field.name + ":" + field.value } } }); $(':checkbox').each(function() { formValues = formValues + "-" + $(this).attr("checked") }); $(':radio').each(function() { formValues = formValues + "-" + $(this).attr("checked") }); $(':file').each(function() { formValues = formValues + "-" + $(this).val() }); return formValues } function ResetOnBeforeUnloadFired() { onBeforeUnloadFired = false } function handleOnBeforeUnload(event) { if (DISABLE_ONBEFOREUNLOAD_PLUGIN) return; if (!onBeforeUnloadFired) { onBeforeUnloadFired = true; if (IgnoreNavigateForEventSource) { IgnoreNavigateForEventSource = false; return } window.setTimeout("ResetOnBeforeUnloadFired()", 10); userValue = GetFormValues(); if (userValue != initialValue) { if (NAVIGATE_AWAY_MESSAGE == "") { return "The changes you made will be lost if you navigate away from this page." } else { return NAVIGATE_AWAY_MESSAGE } } } }
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.