views:

930

answers:

4

How to call a javascript function on body load of a content page? As I can not use the body load of my Master page body onload in this.

A: 

Take a look at this

Gary Green
+2  A: 

If you're using ASP.NET Ajax, then you can add a load handler on your content page:

Sys.Application.add_load(contentPageLoadHandler);

function contentPageLoadHandler() {
    // do stuff
}

Using this approach, every content page or even every control, can setup its own load handler. See this page for more information.


Alternatively, if you're using jQuery, you can achieve the same by using this approach (on your content page):

$(document).ready(function() {
  // do stuff
});

See this page for more information.

M4N
If you're using the DOM, whilst less accurate you can do document.onload() = function() { }
Kezzer
@Kezzer: if you use that approach on both the master page *and* the content page, only one of them will be executed!
M4N
+1  A: 

You need to register a StartupScipt in your master page

Page.RegisterStartupScript("name", script);
cgreeno
A: 

Thanks a lot.

Successful attempt by using window.onload

window.onload=function(){ foo(); }

Chetan
Please note: if you have this code on both the master page *and* the content page, only one of them will be executed!
M4N
this is really bad - assigning directly to onload overwrites any previous value. The recommended practice is addEventListener/attachEvent (http://www.quirksmode.org/js/events_advanced.html) which any library will encapsulate for you.
annakata