Chetan
Chetan

Reputation: 1567

Body onload handler in a content page

How do I call a Javascript function on the body load of a content page? As I can not use the body load of my master page's body onload in this.

Upvotes: 3

Views: 8686

Answers (4)

Chetan
Chetan

Reputation: 1567

Successful attempt by using window.onload:

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

Upvotes: 1

clue
clue

Reputation: 11

It seems that you already handled the issue time ago, just to the googlers, in fact you can use the onload from your master page, the con' is that you'll have to put the same name of the javascript function you call on everypage, maybe empty in some cases...

Upvotes: 1

cgreeno
cgreeno

Reputation: 32391

You need to register a StartupScipt in your master page

Page.RegisterStartupScript("name", script);

Upvotes: 1

M4N
M4N

Reputation: 96576

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.

Upvotes: 2

Related Questions