Warn user before leaving page with unsaved changes

M

markalroberts

Hi,

I wish to ask warn the user that there are unsaved changes (if there
are) and allow them to cancel navigating away/closing the browser.

Investigation leads me to believe the "OnBeforeUnload" event is the
best bet - this does as I wish... Except it doesn't distinguish between
hyperlinks within the site and hyperlinks outside it.

I.e. simple Hyperlink button will raise the Unload event when clicked,
as will closing the window, but I'm only interested in the Closing
window one, and there doesn't appear to be any way to tell them apart.

If someone could help here, it would be very useful!
Thanks,
Mark.
 
S

sp3d2orbit

I've done something similar to that. Ignoring the hyperlinks and only
responding to a close involves some manual coding. Start with this
block of javascript.

var IsLinkClicked = false;

function CheckUnload()
{
if (!IsLinkClicked && ("1" ==
document.forms["Form1"].IsChanged.Value))
{
if (confirm("You have unsaved changes. Click OK to save changes."))
{
document.forms["Form1"].submit();
alert("Changes saved.");

return false;
}
}
}

First thing, you'll want to set a hidden fields 'IsChanged' to true
whenever you make a change that should be saved. This way you'll only
prompt when a change has actually been made. Secondly, if the user
clicks on any hyperlinks on your page you need to set 'IsLinkClicked' =
true using javascript whenever a user clicks on a link. That way you'll
only prompt when the user closes the window or manually types in a new
address into the URL bar.

Instead of a hidden field for IsChanged, you could probably implement
this as a javascript variable also.
 
G

Guest

When it is a planned navigation (like hiperlink , button etc) set a
javascript variable and check this variable in OnBeforeUnload to determine
whether to display warning message or not ..

for eg, declare javscript variable

var warn_pageunload = true;

Fron button click , link click etc set

warn_pageunload =false;

In the unload event

if(warn_pageunload == true)
{
\\code to warn user
}
 
Joined
Jan 27, 2010
Messages
27
Reaction score
0
Warning the user before leaving the page !!!

You might have seen in many of the web form pages to warn the user before closing the page.When somebody refreshes the page, then there is a chance for loosing all filled data. In that case it is very helpful.
Following is the code :



<script type="text/javascript">
var shouldsubmit = false; // Represent the button should ask for confirmation or not.
window.onbeforeunload = confirmUnloading;



// Funcion called before unloading of the page.
function confirmUnloading()
{
if (!shouldsubmit)
{
return "If you have any unsaved data in the current page, it will be lost.";
}
}
</script>


<asp:Button ID="btnSubmit" runat="server"
Text="Submit"
CssClass = "btnPrimary"
OnClientClick="javascript:shouldsubmit=true;"
ToolTip="Submit Page"/>

Thanks
 
Joined
Jan 27, 2010
Messages
27
Reaction score
0
Warning the user before leaving the page

ou might have seen in many of the web form pages to warn the user before closing the page.When somebody refreshes the page, then there is a chance for loosing all filled data. In that case it is very helpful.


Page life cycle includes two events like onunload and onbeforeunload. For this case you need to bind the script function in window. Onbeforeunload so that it will be called when page is unloading.

Again this warning should not be fired when you are actually submitting the page. For that set a boolean value (e.g. shouldsubmit) to submit the page.

Following is the code :



<script type="text/javascript">
var shouldsubmit = false; // Represent the button should ask for confirmation or not.
window.onbeforeunload = confirmUnloading;
 
Joined
Feb 28, 2011
Messages
1
Reaction score
0
Hi,

How to add this code for checkbox and dropdownlist instead of button.


You might have seen in many of the web form pages to warn the user before closing the page.When somebody refreshes the page, then there is a chance for loosing all filled data. In that case it is very helpful.
Following is the code :



<script type="text/javascript">
var shouldsubmit = false; // Represent the button should ask for confirmation or not.
window.onbeforeunload = confirmUnloading;



// Funcion called before unloading of the page.
function confirmUnloading()
{
if (!shouldsubmit)
{
return "If you have any unsaved data in the current page, it will be lost.";
}
}
</script>


<asp:Button ID="btnSubmit" runat="server"
Text="Submit"
CssClass = "btnPrimary"
OnClientClick="javascript:shouldsubmit=true;"
ToolTip="Submit Page"/>

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top