Solved

javascript onbeforeunload event

Posted on 2010-09-13
7
269 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
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now