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.
+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
2009-02-09 15:08:04
If you're using the DOM, whilst less accurate you can do document.onload() = function() { }
Kezzer
2009-02-09 15:20:49
@Kezzer: if you use that approach on both the master page *and* the content page, only one of them will be executed!
M4N
2009-02-09 15:38:48
+1
A:
You need to register a StartupScipt in your master page
Page.RegisterStartupScript("name", script);
cgreeno
2009-02-09 15:09:03
A:
Thanks a lot.
Successful attempt by using window.onload
window.onload=function(){ foo(); }
Chetan
2009-02-09 15:15:57
Please note: if you have this code on both the master page *and* the content page, only one of them will be executed!
M4N
2009-02-09 15:37:56
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
2009-02-23 09:55:48