Click here to Skip to main content
15,893,161 members
Articles / Web Development / ASP.NET

Work smarter not harder: “Navigate Away” feature for your ASP.NET/other web applications made easy

Rate me:
Please Sign up or sign in to vote.
4.91/5 (33 votes)
14 Dec 2010CPOL10 min read 113.5K   900   63  
A jQuery script that lets you display the “Navigate Away” browser message when an input field value is changed in the page, and for that, you don’t need to write any code!
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder SmartAspects
Bangladesh Bangladesh
I write codes to make life easier, and that pretty much describes me.

Comments and Discussions