onBeforeUnload doesn't work quite the way I want it too

  • Thread starter Richard \(MrBonus\)
  • Start date
R

Richard \(MrBonus\)

the code is taken from my asp.net page. (written in C#)

Hi, im trying to make my page postback before the it unloads, i've tried a
lot of stuff, and finally I made something that worked, the problem is the
alert in the code -- alert('TittyX'); --. If I remove it, then it doesn't
work, well the method runs fine, but I can't catch the --
theform.submit(); --.

Another weird thing, is I can only catch the event in the Page_Load event on
the server, which is weird, but I can live with that, what I can't live with
is the alert.

Anyone who knows either another solution, or can tell me what I should do to
make it work?


<body MS_POSITIONING="GridLayout" onbeforeunload="__doUnloadPostBack()"
language="javascript">
<script language="javascript">
<!--
function __doUnloadPostBack() {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape")
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.submit();
alert('TittyX');
}
// -->
</script>

<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 184px; POSITION:
absolute; TOP: 120px" runat="server" Text="Button"></asp:Button>
</form>
</body>


Richard
 
R

Robert

Richard \(MrBonus\) said:
the code is taken from my asp.net page. (written in C#)

When posting to this forum, it is best to go into the web browser and
post a copy of the gernated code. Folks like me do not know asp nor
C#.
Hi, im trying to make my page postback before the it unloads, i've tried a
lot of stuff, and finally I made something that worked, the problem is the
alert in the code -- alert('TittyX'); --. If I remove it, then it doesn't
work, well the method runs fine, but I can't catch the --
theform.submit(); --.

I suspect that you have a timing problem. I have read where there may
be a timing problem when you cancel the page after submitting the
page.

Here is an example of doing some validation on the client then
submitting the form if validation is successful.

I tried this on IE and Netscape on MacOS 10.2.6.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
<head>
<title>Check form fields</title>

<script type="text/javascript">

function validate()
{

var x = document.forms["myForm"];
var submitOK = true;
var messageText =
"What a suprise! Please correct the error in your ways." +
" Please remove the asterisk ( * ) in the name field.";


if (x.theName.value.indexOf("*") >= 0)
{
alert(messageText);
submitOK = false;
}

return submitOK;

}

</script>

</head>
<body>
<p>
This html file shows how to cancel a submit.
To cancel the submit, place an asterisk ( * ) in the name field.
</p>
<form name="myForm"
action="http://www.natAValidWebAddress.com"
method="POST"
onsubmit="return validate();">
<p>Name:<br>
<input type="text" name="theName" size="20"><br><br>
<p><input type="submit"></p>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top