[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

javascript onbeforeunload event

Posted on 2010-09-13
7
Medium Priority
?
285 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
5 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 1000 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses
Course of the Month19 days, 23 hours left to enroll

873 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