Solved

onbeforeunload with condition

Posted on 2006-07-23
3
559 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
[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
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

Industry Leaders: 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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