303 - Blastoff (Rockumentation)

JavaScript and Partial Postbacks

There can be some confusion over when to use $(document).ready() vs pageLoad() vs add_init() in JavaScript. It really depends on the situation. The following summary from this old article should help. If you need more details see this MSDN article on AJAX Client Life-Cycle Events.

Using $(document).ready()

  • Ideal for one-time initialization.
  • Optimization black magic; may run slightly earlier than pageLoad().
  • Does not re-attach functionality to elements affected by partial postbacks.

Using pageLoad()

  • Unsuitable for one-time initialization if used with UpdatePanels.
  • Slightly less optimized in some browsers, but consistent.
  • Perfect for re-attaching functionality to elements within UpdatePanels.

Using Sys.Application.add_init( function(){} )

  • Useful for one-time initialization if only ASP.NET AJAX is available.
  • More work is required to wire the event up.
  • Exposes you to the “sys is undefined” error if you aren’t careful.

app.add_load(ApplicationLoad);

app.add_init(ApplicationInit);