Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a number of projects that could benefit from JavaScript. I've seen a lot of "Oh, you can use JavaScript for that!", but little detail about how and where that's done. Does the script live on a server somewhere or is it just embeded with a cut-and-paste via the "edit HTML source" button?

Looking for some guidance on the best place within the sites/farms from which to run scripting.

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Technically, the scripts can reside anywhere as long as the users have read access to them.

In practice, client side solutions usually have a short lifecycle, and you'll certainly want to place them in a location that you can easily update. A good option is the Style Library within a site collection, which has the added benefit of caching the files. If you have internet access, you could also consider using a CDN to call popular files (like jQuery.js).

On the pages themselves, you can add the scripts to either the master page or via a Web Part, depending on the scope of your customization. Here are some explanations from my blog if you use Web Parts:

http://blog.pathtosharepoint.com/2010/10/27/about-scripts-web-parts-and-urban-myths/

[Update] I just came across this fresh article:

http://techtrainingnotes.blogspot.com/2012/05/adding-javascript-and-css-to-sharepoint.html

Note that the above links only show some basics. There are several more ways to include scripts, especially if you work with Master pages.

share|improve this answer
add comment

The best approach is to create a Scripts document library, preferably with versioning enabled to store your JavaScript files. Then on the pages you'd like to use them, add a Content Editor web part to the page and use the link box to point to your javascript files.

share|improve this answer
add comment

There are several ways to add JavaScript to SharePoint. You could add directly or reference it into the masterpage, page layout, code behind etc.. The script lives always on a server, because it serves it.

Keep in mind that JavaScript is different language to learn and can be very powerful. Therefor I recommend learning it:

http://www.codecademy.com (Nice website where you can learn the fundamentals) https://developer.mozilla.org/en/JavaScript/Guide (Very comprehensive information)

Besides learning the language it's recommended to place JavaScript into files, so you can reuse and manage your code. The Style Library is a good place for the *.js files, also such as for the style sheets (css). Besides learning JavaScript I would also recommend to have a look at jQuery.

share|improve this answer
3  
+1 for good links! I'd also like to add, that IF you have access to the server, add your javascript files within the Layouts folder as you can always reach that folder regardless of permission settings. Example: /_layouts/<foldername>/<javascript filename>.js. Only disadvantage being you'll always need to know the right path as it isn't browsable via the SharePoint UI. –  Daniel Ziga May 8 '12 at 13:18
2  
While I'm a fan of how Dandroid describes storing the JavaScript, I'd add another disadvantage - the .js file should be deployed into /_layouts by a Web Solution Package (.wsp) so it is automatically copied to all servers, and that's a little more complicated. If you put the .js file into the Style library that deployment isn't necessary. –  Andy Burns May 8 '12 at 16:59
    
+1 for pointing out that JavaScript is different to C#. It doesn't work when developers assume they can use it like they would C#. I'd also add javascript.crockford.com though he'll leave your head spinning at times. –  Mike H May 24 '12 at 2:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.