I’ve written a web part that can help you insert JavaScript into a SharePoint page. Currently there is nothing stopping you using a content editor web part, but it has a few limitations. First is the fact that the JavaScript doesn’t stand out, people may think that the content inside the editor is blank, when in fact it contains JavaScript.
By having a dedicated web part for JavaScript it becomes clearer that JavaScript lives on the page, also we can add a few features that make working with JavaScript a little easier.
I’ve made the chrome state set to None by default, so you won’t see the web part at all during normal render time (only design time).
The properties:
Page load JavaScript: This can be any JavaScript that you want to run when JQuery loads, that is any code you want to live inside of:
$(document).ready(function(){});
Something cool to try out (from EndUserSharePoint) try adding: $('#LeftNavigationAreaCell').toggle(); This will remove the left hand navigation.
Page level JavaScript: This is JavaScript that you just want to live on the page, it could be globally scoped variables or some functions that you have defined.
Script Includes: Each new line can be the URL to a JavaScript file to be included in the page, this is particularly useful for including JQuery plugins.
Use Google Libraries: Just a little novelty, it will use the Google Ajax API’s to load JQuery instead of the embedded JQuery resource.
You can have multiple web parts on the same page, the best bit about this is that all the code will be output into one place, so if you have one web part with some page load JavaScript that has say: alert(‘load’); and anther that includes the left nav cell hide from above, the result in the page would be:
$(document).ready(function(){alert’load’); $('#LeftNavigationAreaCell').toggle(); });
After you deploy the solution, be sure to activate the feature under ‘site features’:
You can download the solution package from here.