Call Javascript Function on Postback

I ran into this problem when I had to call a javascript on page load after my server-side code threw an exception to the user.

If you want to call a javascript function on page postback, you'll need to add an EventListner to the current document.

 

<script language="JavaScript" type="text/javascript">

function doSomething(){.... }


if (window.addEventListener)
    window.addEventListener("load", doSomething, false);
else if (window.attachEvent)
    window.attachEvent("onload", doSomething);
else if (document.getElementById)
    window.onload= doSomething;

</script>

Published Tuesday, February 5, 2008 4:55 PM by Kiyoshi
Filed under:

Comments

# re: Call Javascript Function on Postback

Friday, August 8, 2008 9:23 AM by Yogi

this blog was helpful to me

# re: Call Javascript Function on Postback

Saturday, October 4, 2008 12:49 AM by Chaitanya

Hey thats great... it worked. I am using frames and depending on the menu selected in to drop down I am changing the link to open page in left frame. For this I have to send query strings which was re initializing when page was auto postback. But above code solve my problem. Thanks a lot.

# re: Call Javascript Function on Postback

Sunday, December 6, 2009 10:56 PM by valar

Thanks for the piece of code. Helped me a lotttttttttt....

# re: Call Javascript Function on Postback

Wednesday, March 17, 2010 8:50 AM by Raju

Thanks for sharing your thought.. Really it is very helpful.

# re: Call Javascript Function on Postback

Sunday, May 16, 2010 3:01 PM by Andrew

Having trouble firing this when postback occurs.  I only want a function to fire when it is a fresh page load not on the autopostback of a control.

# re: Call Javascript Function on Postback

Friday, March 4, 2011 8:36 AM by vikram

thanks a lot... this helped me... this is exactly what i wanted... :-)

# re: Call Javascript Function on Postback

Wednesday, June 5, 2013 2:58 PM by xelaDGlITruL

RXeWGE Wow, great article.Really looking forward to read more. Cool.

# re: Call Javascript Function on Postback

Tuesday, October 15, 2013 1:59 PM by DkMxwXEvbxwy

iObLMO Major thanks for the article.Much thanks again.

Leave a Comment

(required) 
(required) 
(optional)
(required)