SharePoint pages are the combination of a master page and content pages. Master page contains the “body” element and content pages are used to display the content. Because of this design you can not directly add a function to the body’s onload event.
SharePoint provides the “_spBodyOnLoadFunctionNames” array. When the body is loaded, the onload event handler executes every function whose name is contained in this array.
To execute “test” method, add this to the array so when body’s onload event fires this method gets executed:
<script language="javascript">
_spBodyOnLoadFunctionNames.push("test");
function test()
{
alert(‘hello’);
}
</script>
_spBodyOnLoadFunctionNames.push("test");
function test()
{
alert(‘hello’);
}
</script>
If you wants to execute any function on every page then add this function to master page.
if you wants to execute this function on a specific page then add a content editor web part on the page and put the function in that web part.