Thursday, July 12th, 2007

Adding Javascript functions to the OnLoad event



This posting is a bit of a note to self, apologies if you’ve seen this before (and you probably should have as Official SharePoint blog should be on your feed list!).

This post shows how the SharePoint design team have used Javascript to default the selected value in a drop down list based on a value passed in the query string.  The bit that I was most interested in, and have seen little mention of anywhere else – or at least when I first looked, was the _spBodyOnLoadFunctionNames array.

 _spBodyOnLoadFunctionNames

In most cases SharePoint pages are based on a master page that contains the “body” element. These content pages can’t directly add a function to the body’s onload event. In order to work around this limitation, SharePoint provides the “_spBodyOnLoadFunctionNames” array. When the body is loaded, the onload event handler executes each function whose name is contained in this array.   So you can add any functions and push them into the onload event, cool.

_spBodyOnLoadFunctionNames.push(”MyJavascriptFunction”);

blog comments powered by Disqus