Tuesday, February 22, 2011

Execute a JavaScript onload() function in SharePoint page

In SharePoint, most of the time master page contains the BODY tag, now if you want to add your JavaScript function on body onload() then there is a workaround for it. SharePoint provides the "_spBodyOnLoadFunctionNames" array. When the body is loaded, the onload event handler executes each function whose name is contained in this array.

<script type="text/javascript">
    _spBodyOnLoadFunctionNames.push("FunctionName");
    function FunctionName()
    {
      // Code goes here...
    }
script>

This array is basically a part of init. If you open the JavaScript file, you will notice the page onLoad event is handled by function _spBodyOnLoadWrapper, which further calls ProcessDefaultOnLoad, and this function executes all the function names added in array _spBodyOnLoadFunctionNames.

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. thanks this was a great help.

    ReplyDelete