Solved

onbeforeunload with condition

Posted on 2006-07-23
3
555 Views
Last Modified: 2008-02-01
In ASP.NET 2.0 with C#
I want to use the command onbeforeunload in order to check if the user presses the close button:
<body onbeforeunload="return ' '">
but this command shows the message each time the page is refreshed.
Can I give a condition that it will not appear in each refresh?
for example the following code check if the value in the page was changed and only if true the onbeforeunload
will active.
I need to do it in .NET and check if the values in the formview in the page were changed and not in each
refresh of the page.
How do I do it?

<head>
<script>
var oldVal = "";
function saveValues()
{   oldVal = document.myForm.tester.value;}

function checkChanges()
{  if ( oldVal != document.myForm.tester.value )
   { return 'Are you sure you wish to exit dude?';}}

</script>
</head>
<BODY onload="saveValues();" onbeforeunload="return checkChanges();">
<form name="myForm">
<input type="text" name="tester">
</form>
</body>
0
Comment
Question by:ANAT2403
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17162961
Here is an option:

-----------------------------------
On Any Forms Want to detect
-----------------------------------
detect close ie X button ( close window)
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4878&lngWId=10

<HEAD>
<SCRIPT language="JavaScript" >
function detactCloseWin() {
    var top=self.screenTop;
     if (top>9000) {
        window.open("closeWindow.aspx","_blank","fullscreen=0,width=0,height=0,
                             scrollbars=0,toolbar=0,menubar=0,status=0");
    }
}
</SCRIPT>
</HEAD>
<BODY onunload="detactCloseWin();" MS_POSITIONING="GridLayout">

-------------------
On new window
-------------------
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
     General.chat.Delete_ActiveUser()
     Response.Write("<script language='javascript'> { window.close();}</script>")
End Sub

Bob
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

11 Experts available now in Live!

Get 1:1 Help Now