Tuesday, May 7, 2013

Using JavaScript In OnLoad Event Of Body SharePoint 2010

When developing web parts or other SharePoint controls many times we might require to call some functions on body onload event. As web part does not contain body tag we cannot use onload attribute of body tag.

To overcome this drawback, a developer can use an array i.e. _spBodyOnLoadFunctionNames
The only thing we need to do is to pass the function's name using .push method.

Example:
<script language="javascript">
_spBodyOnLoadFunctionNames.push("TestFunction");

function TestFunction() 

   alert("hello"); 

</script> 

We can use above script in master page, aspx page, content editor web part, visual web part as per the need.

Contact Me or post a comment for any technical help related with SharePoint 2010.

No comments: