« The Best Way to Add Custom CSS to SharePoint | Main | Office Space »

01/31/2010

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Christophe

And of course this is not THE solution, let alone the best way, to add custom JavaScript or jQuery to SharePoint. This is just a solution.
For example your way is not recommended:
- when you just use jQuery on one specific page
- when you only need jQuery after the page has loaded
- when you are not even sure if the user will use your jQuery customization on the page (e.g. tooltips)
- etc.

Brandon Anderson

Christophe, correct, this solution assumes jQuery is used throughout the site. If you want to use jQuery on specific pages, you can include the jQuery references from the post on specific page layouts in the PlaceHolderAdditionalPageHead content place holder.

Ming Jones

Can the tag be placed in a content placeholder? What would you do if you only had 1 page that needed to call jquery and that page is a common page layout but the jquery isn't need on any other page?

Brandon Anderson

You can reference the script tag from the head tag, like shown, or anywhere in the body. If I need to call the jQuery from only a particular page, I'd add a Content Editor Web Part (CEWP) to a web part zone. I'd edit that CEWP in HTML view and add my script references (or the scripts themselves). That way the jQuery only takes place on that page.

Ming Jones

Thanks for that tip - new to SharePoint so I wasn't familiar with CEWP and all this time I thought SharePoint was against users using or tags.

Dhaval

Hi Brandon, let say if I dont have access to the SPD, also Master page on the server,layouts. All that I can do is though Sharepoint GUI and I want to use JQuery on a specific site in a Content Editor Web Part, how should I go about it ? I would appreciate your help.

Brandon Anderson

Dhaval, given your access restrictions, you can add a Content Editor Web Part (CEWP) to the page. Inside of that CEWP, in the HTML editor view, you can drop in the script reference to your jQuery library file like I have in step 8 in the post above. Alternatively, instead of referencing the jQuery library hosted on your server (which you may not have the ability to add) you can reference the jQuery library on Google's Content Delivery Network. The code you would use for that is [lt]script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"[gt][lt]/script[gt].

Now that you have your CEWP referencing the jQuery library, you need to specify what you want to do with jQuery. So, in the same CEWP, in HTML edit view, after your jQuery library reference tag, add another script tag, inside of which you can put your jQuery statements. That would look like this: [lt]script type="text/javascript"[gt]
$(document).ready(function(){
$("p.hide").fadeOut();
// some statement
});
[lt]/script[gt]

Steve Ottenad

Another easy way to include javascript, and still leverage some great stuff from .net/sharepoint is to use the tag. I wrote a little walkthrough here Adding Javascript to a SharePoint 2010 Master page. It allows you to reference your path from the sitecollection or site root. Pretty handy if your master page will be used in subsites.

Brandon Anderson

Thanks, Steve. I appears the link did not come through in the post. I'd be interested to hear your thoughts.

LJ

I discovered another way to potentially add javascript to a custom master page to be used site wide. First, add the js file to your site's Site Pages, Site Assets, or other document library. Second, add a ScriptManager with a ScriptReference. In the ScriptReference's Path attribute, add the following:

[lt]% $SPURL: ~site/{Rest of Path here} %[gt]

In my case, I added jquery-1.4.2.min.js to my site's Site Pages library. The ScriptReference's Path used to reference it is:

[lt]% $SPURL: ~site/SitePages/jquery-1.4.2.min.js %[gt]

The comments to this entry are closed.

SUBSCRIBE BY EMAIL

Contact ICF Interactive

  • ICF Interactive a full-service, interactive agency with the ability to guide brands digitally – through an informed strategy, inspired design, technical know-how and an obsession for humanity, we not only can launch your site but we can digitally catapult your brand. If you have a project in mind, would like information regarding our work, career opportunities, proposal requests or anything else, please feel free to get in touch. We’re ready for what’s next. hello@icfi.com