Tuesday, 31 August 2010

Alternative of using _spBodyOnLoadFunctionNames

I am very sure that most of the Sharepoint developers must have used _spBodyOnLoadFunctionNames to inject javascript methods on the sharepoint list forms(NewForm/EditForm/DispForm).

Downside of using _spBodyOnLoadFunctionNames is the time taken to execute my javascript function. Even though my function is very small, there is delay in executing my method.

Reason for the Delay:

Delay occurs because sharepoint first loads all the controls on the form and subsequently executes the embedded javascript functions. So if number of controls are high, it will take substantial time to execute my custom javascript method. Delay would be more in case of Sharepoint 2010 pages because it has more number of inbuilt javascript functions to execute while loading the page.

Solution:

In order to avoid the delay we have to execute our function "onload" event of any control and following steps can be considered to achieve this functionality.

1. Create your javascript function
function MyMethod()
{
// My method logic
}

2.Create a "img" element anywhere on your page(Since we can not capture "onload event" of most of the controls).
3. Set the image source to "/_layouts/images/blank.gif" so that nothing is visible on the page.

4. Call your javascript function on "onload event of the img.


<IMG SRC="/_layouts/images/blank.gif" width="1" height="18" onload="MyMethod();"/>

Note: Mentioned issue is quite visible in case you are trying to hide/show some buttons/controls of the page on load of the page.

1 comment:

RN said...

Hey Vimalesh..my roomie was looking at your blog for some sharepoint issue he was facing...when i saw your photo..i said proudly that he is my classmate :) good work man..keep it up !!