Monday, August 17, 2009

How to execute javascript onLoad only once in an ASP.NET page

If you are working on a ASP.NET Master/Content Page scenario and have declared a javascript onload, then the script gets executed every on every postback.

However if you would like to execute it only once during the first postback, then here's how to do so:
Copy this code in the of your MasterPage



The code determines if there is a postback in Javascript and then executes code based on the result. Hence the alert popup is shown only once.

You can several other tips related to calling JavaScript in Master/Content Pages over here:

Calling JavaScript from ASP.NET Master Page and Content Pages - Part I

Calling JavaScript from ASP.NET Master Page and Content Pages - Part II

No comments:

Post a Comment