How do i Add onLoad JavaScript event in SharePoint?
SharePoint provides a JavaScript array "_spbodyonloadfunctionnames", any function is executed onLoad needs to is added To this array e.g.
_spbodyonloadfunctionnames.push ("Executemyfunctiononload");
Now, why does your JavaScript function doesn ' t execute if you just register it
With Clientscript.registerstartupscript?
Actually, content pages can ' t execute JavaScript function on the body load, reason; Content pages can ' t directly add a function to the body ' s OnLoad event if master page contains the <body> element (W Hich is mostly true).
This is basically a part of init.js located in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions \12\template\layouts\1033\ ". If you open the JavaScript file and you'll notice the page onLoad event is handled by function _spbodyonloadwrapper, which Further calls Processdefaultonload, and this function executes the all function names added in array "_spbodyonloadfunctio Nnames ".
function Addloadevent (func) {
var oldonload = window.onload;
if (typeof window.onload!= ' function ') {
Window.onload = func;
} else {
Window.onload = function () {
Oldonload ();
Func ();
}}}
Addloadevent (Nameofsomefunctiontorunonpageload);
Addloadevent (function () {
/* More code to run on page load * *
}
);