Solved

javascript onbeforeunload event

Posted on 2010-09-13
7
274 Views
Last Modified: 2013-11-18
Hi All,

I am a new customer to experts-exchange and this is my first question.
I am also fairly new to javascript.

I have a requirement to capture any changes made to a form when a user clicks the browers BACK or FORWARD button and show a warning message.If no edits are made in the form then the warning message should not appear
Can I do it using javascript?
If I can do it can any one tell me what are the events that I have to use?
I have searched google a little bit and undrestand that onbeforeunload does this functionality.
But how would i recognise whether any changes are made to form data in the onbeforeunload event.

Any help is gretly appreciated.

Thank you
Raj
0
Comment
Question by:raju_surya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:pavlejovanov
ID: 33662074
in .Net code
<script language="javascript" type="text/javascript">
window.onunload = function() {
            PageMethods.SomeFunction();
        }
</script>

in C# code

[WebMethod]
public static void SomeFunction()
{
        //Some code
}

you also must have ScriptManagerr in page
0
 
LVL 29

Assisted Solution

by:Badotz
Badotz earned 250 total points
ID: 33662449
You don't have to use .NET:


<script type="text/javascript">

function last_chance() {
	return "Are you sure you want to leave?";
}

window.onbeforeunload = last_chance;

</script>

Open in new window

0
 
LVL 22

Accepted Solution

by:
kadaba earned 250 total points
ID: 33663191
You might have to use onchange event on the form fields to record the change probably have flag and then use the above syntax.... I mean roughly you could do this way..

<html>
<head>
<title>Capture Changed fields</title>
<script type="text/javascript">
function last_chance() 
{	
	if(isEdited)
	{	
		return "You seem to have edited some information, Are you sure you want to leave?";
	}
}

var isEdited = false;
function captureEdit(){
	isEdited = true;
}

window.onbeforeunload = last_chance;
</script>
</head>
<body>
Name: <input type="text" id="name" value="EE" onchange="captureEdit();">
<br>
Email: <input type="text" id="email" value="ee@ee.com" onchange="captureEdit();">
</body>
</html>

Open in new window

0
 
LVL 6

Expert Comment

by:mickey159
ID: 33690450
Try this:
<script language="JavaScript">
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "You have attempted to leave this page.  If you have made any changes to the fields without submitting, your changes will be lost.  Are you sure you want to exit this page?";
  }
</script>
0
 
LVL 29

Expert Comment

by:Badotz
ID: 33690542
mickey159: Your script tag is outdated ("language=" should be "type="); your function comes AFTER it is referenced (possibly inconsequential); your example adds nothing new (except the prompt, of course, but otherwise...).
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question