Mar 21, 2009

Add Javascript on SharePoint pages

Some tips to inject javascript into sharepoint page:

1) _spBodyOnLoadFunctionNames is a good friend. It will be called on page onload. So to make a javascript function call, you just need to put this

_spBodyOnLoadFunctionNames.push('myJsFunction');

and your function definition in a CEWP on any sharepoint page.

2) toolpaneView=2 is the trick to insert CEWP on list forms such as Edit, View, New.

3) delegate control with Id ="AdditionalPageHead" to add script cross pages.

Implement this control with a user control in the /controltemplates, and the user control can control nothing but your custom javascript files under _layouts. For example, to include JQuery library:

<%@ Control % >
< script src="./_layouts/jquery-1.2.6.js" type="text/javascript" / >

To use custom CSS files without modifying master page, see this post.

Update 04/13/2010:
4) Use CustomAction in Sharepoint 2010.