Is there a javascript event that will always fire upon postback?

J

john

I would like to do some client side stuff whenever my page is posting
back. My page has a lot of different buttons and links that will cause
a postback. I would rather not have to put something in each of their
"onclick" functions. I would rather have a global OnPostback function
or something like that. I tried using the form's onsubmit function,
but it only gets called when my buttons are clicked; it doesn't get
called when a link gets clicked or a dropdown list causes a postback.
I noticed that these things call the ASP.NET javascript function
__doPostBack. I don't know if I could somehow modify the __doPostBack
function to call my function that does stuff before a postback. Any
ideas?
thanks in advance.
 
P

Peter O'Reilly

Try:

document.MyForm.submit();

within one of your client side DOM object's onClick event procedure.
 
S

Scott

The easiest way I know (it's a hack) is to forward the _doPostBack to my own function and do what I
need in that new function then call _doPostBack (make sure you always force a the _doPostBack
generation on a page, the framework is clever enough to not include it sometimes).

Something like:


<script language="javascript">
<!--
function MyDoPostBack(eventTarget, eventArgument)
{ // do local stuff
if (theform.onsubmit == null || theform.onsubmit()) {
return __oldPostBack(eventTarget, eventArgument);
}
}
var __oldPostBack = __doPostBack;
__doPostBack = MyDoPostBack;
// -->
</script>
Scott
 
J

john

Thanks for the response. I actually found a couple of javascript
events that fire when i want them to: onstop and onbeforeunload. But
they might only be available for IE.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top