Welcome to Comunidade Bloggers |create|it| Sign in | Join | Help

André Vala

SharePoint
PerformancePoint
.Net

News

  • Vote for us in theEuropean SharePoint Community Awards 2012
    Locations of visitors to this page

    Comunidade Portuguesa de SharePoint

    View André Vala's profile on LinkedIn

    © André Vala and Create IT, 2006-2011. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to André Vala and Create IT with appropriate and specific direction to the original content.

SharePoint Tip #19: Using JavaScript in Body OnLoad Event

When developing web parts or other controls that use JavaScript, it's quite common to need to call some function on the Body OnLoad event of the web page. The problem with this is that, in SharePoint, the Body element is in the master page and not in the control, so doing this is can be tricky.

Fortunately, the SharePoint team gave the developers a way to do this easily, using a special array called _spBodyOnLoadFunctionNames. The only thing you have to do is push your function's name to this array and it will get called in the OnLoad event of the window.

Example:

_spBodyOnLoadFunctionNames.push("MyFunction");

function MyFunction() {
    setTimeout('MyFunction()', 1000);
}

Posted: Sunday, December 16, 2007 6:30 AM by andrevala

Comments

Leo said:

WHere do you write this peiece of code in an aspx page
# January 7, 2010 11:27 AM

andrevala said:

You place this in the JavaScript file. SharePoint's javascript code defines that array so that you can use it in your code.

# January 8, 2010 5:41 AM

Fox said:

Just in case someone was wondering why the above code didn't work. Use Function with lowercase , like so : _spBodyOnLoadFunctionNames.push("MyFunction"); function MyFunction() { setTimeout('MyFunction()', 1000); }
# August 19, 2010 10:49 AM

andrevala said:

Corrected. Thank you for your comment.

# August 21, 2010 5:44 PM

priya said:

Is it necessary to set timeout period if we are using the array _spBodyOnLoadFunctionNames.

As you mentioned in the method definition.

setTimeout('MyFunction()', 1000);

# April 7, 2011 12:59 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS