2002 was a great year……
MJ was still in the league, Phil Jackson wrapped up his third three-peat with the Lakers and the playoffs were entertaining. Who doesn’t remember a dominant and relevant Shaq referring to the Kings as the Sacramento ‘Queens’ and being entertained with another game seven in the Western Conference Finals.
Technology was no stranger to controversy as blogs and books were being released regarding web performance tuning tips. Some authors even went so far to suggest excluding or minimizing the use of JavaScript and style sheets. Published in 2002….”There are some nice features to both JavaScript and style sheets, but at a big cost. Life is better without them.” Top Ten Web Performance Tuning Tips http://www.oreillynet.com/pub/a/javascript/2002/06/27/web_tuning.html
Combining the high cost of hardware with the somewhat slow speeds of internet access, developers in the early 2000s had to be aware of how long their web pages would render for their visitors. The performance concerns that developers had back then are still relevant today.
Technology and connectivity might be considered leaps and bounds advanced from the period mentioned earlier, but visitors are requesting information using multiple devices, including cellphones with spotty reception and tablets using shared slow connection at a coffee shop. You have to be aware of how ‘heavy’ your pages are and how many resources are needed to display your pages. There are several tactics to improve performance of web sites and pages, but let’s concentrate on the inclusion of JavaScript in pages and more importantly how they are included inside SharePoint.
For the past year or so, the hottest trend with SharePoint is to include a personal favorite JavaScript library in SharePoint. Most actions inside SharePoint involve a page load which typically translates to a physical screen flicker to your visitors. In an effort to make pages reflect a more modern feel, developers have been using JavaScript to dynamically alter and add data to a page without posting the page back to the server. Developers can either write their own script or include a framework that encapsulates common functions. One example of a framework that is unique to SharePoint is SPServices. SPServices has exploded with popularity and has been downloaded over 61,000 times. Outside of SharePoint, jQuery is extremely popular with developers to ‘simplify the client-side scripting of HTML’.” jQuery tools” is another open source framework that is built using the jQuery library to supply even more client side functionality. Combined together, these frameworks are just a few examples that have been used to unlock SharePoint from the client side.
There are a few options to include scripting into sites:
- Alter the Master Page to include JavaScript
- Create or Alter a Page Layout using Visual Studio or SharePoint Designer (SPD)
- Use Content Editor Web Parts to include JavaScript
To complicate this further, there are different techniques for including JavaScript:
- Inline scripting injected inside a script tag
- Reference to a script file inside your site
- Reference to a Content Delivery Network (CDN)
Using JavaScript in your Custom Master Page
It is common to make a JavaScript reference inside the master page used by your SharePoint site. The image below shows three script files are being included inside the head tag of a master page.
Positive:
The benefit of this approach is that now the scripts will be available to any resource that uses the master page. As opposed to altering numerous pages to include a script file, you now have to make one entry in the master page.

Negative:
When visiting Alaska, there is no need to pack that surf board. If scripting is used for one webpart that is needed on only one page, then it is unnecessary to have this script load for every other page inside your site. Including the script on the master page will ensure that the file is loaded into the browser whether or not the scripting is actually used.
Using JavaScript in your Page Layouts
To alleviate the issue of loading only necessary components, page layouts are used to reference scripting when a specific page type is loaded by a visitor. An easy approach to this is to create a new page layout using SharePoint Designer 2010 and include your script inside a content region with an ID of PlaceHolderAdditionalPageHead.
Positive:
Using JavaScript page layouts ensures that the scripts are called only when a specific page is loaded by your visitors. With this method the pages that do not use this page payout should weigh less. As an added bonus, developers can skip using SharePoint Designer and utilize Visual Studio 2010 to create page layouts. Using Visual Studio 2010 gives you the flexibility to deploy page layouts as a feature as opposed to manual edits inside SharePoint Designer 2010. To learn more about this technique visit the follow blog: “Every Problem has a solution, Even SharePoint Branding.” (http://www.sharepointcat.com/2011/08/every-problem-has-a-solution-even-sharepoint-branding/)

Negative:
Implementing page layouts implies a baseline knowledge of content types and planning as to where or what types of pages are needed within your site. Perhaps that should not be considered a negative when in essence it forces you to slow down. For all the copy and paste coders out there, it is assumed that you have some HTML knowledge for inclusion of your JavaScript.
Using JavaScript in a CEWP
Saving the best for last? The last option for including script inside SharePoint involves using a Content Editor Web Part (CEWP) to link to a text file that contains your custom script.
Positive:
None. This abomination of an approach essentially does the same as using a page layout by ensuring that the script is only loaded on a specific page.
Negative:
By not paying attention to permissions or communication, an unknowing page editor could delete this web part from the SharePoint page which could then cripple the entire page. Perhaps this scenario is unlikely for the masses but it is fair to say that using CEWP is not even close to being robust as page layouts. When new pages are created, the creator can select your custom page layout that includes custom scripting. I would much rather have my scripts propagated inside a reusable container as opposed to manual entries to individual pages.
JavaScript Techniques
Inline script
Inline scripting means inserting your JavaScript inside a script tag. Using this approach means that you will have to update whatever mechanism was used to introduce the script whether it be the master page or page layout. Having to update the entire master page just to make a scripting change seems like over kill.

Script Files
Using files that contain custom script provides another layer of flexibility. This enables developers the ability to alter the script directly as opposed to editing master pages or page layouts. Putting the files in a location that developers can access without relying on SharePoint Designer 2010 could also be viewed as a win especially in the scenarios where access to SharePoint Designer is locked down.

There are two types of methods to include JavaScript files with the script tag or with a SharePoint ScriptLink control. The old method uses the script tag with new approach using the SharePoint ScriptLink control.
<script src=”/Style Library/custom/jquery-1.4.4.min.js” type=”text/javascript”></script>
<SharePoint:ScriptLink ID=”ScriptLink1″ Name=”~sitecollection/Style Library/custom/JS/jquery-1.4.4.min.js” runat=”server” />
MSDN: ScriptLink Class http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.scriptlink.aspx
Content Delivery Network (CDN)
Inside the script tag, instead of referencing a relative path to an internal resource you can point to a publicly accessible file hosted on a Content Delivery Network (CDN), for example: http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.js. The benefit of this approach is that it will help out with performance. In regards to global visitors, using a CDN will dynamically determine which hosting server is closest to the request and serve content accordingly. Pay attention to this especially when thinking of Asia or Europe visitors who would normally have to download resources from US based servers. The glaring downfall to using a CDN is that it could be considered malicious to some corporate networks that could have this traffic blocked by their firewall.
Conclusion
There are several options for including custom scripts inside your SharePoint farm. Your decision will impact how quickly content is rendered to visitors. It is up to you to make a qualified decision as to which approach fits your corporate identity. Do you have development resources? Is this for a Proof of Concept site? Do security restrictions limit access to external sources? This blog post should outline what options are available to you with additional resources provided below.
Extra Credit Reading
When working with JavaScript files, you probably have wondered why there are full versions and minified versions of the same JavaScript file. This is another technique to help out with performance by essentially shrinking the physical size of the files. The smaller the JavaScript file is…the quicker the download is on the browser for your visitors. Paul Schaeflein has a great write up on this process in his blog: Minifying JavaScript Files with Build Events (http://www.schaeflein.net/Pages/Minifying-JavaScript-Files-with-Build-Events.aspx)
Lastly there is a CodePlex solution worth evaluating called SP SIN (http://spsin.codeplex.com/). This allows your SharePoint site to load in both CSS and JavaScript files based on the inclusion of words in the URL. There are several benefits to using this approach since it allows alterations without having to edit any pages or master pages. In my opinion it takes the benefits that you would get by using separate page layouts but centralizes the management of resources inside one SharePoint List.
References
Creating a Page Layout in SharePoint 2010 using Visual Studio 2010 (http://blog.beckybertram.com/Lists/Posts/Post.aspx?ID=71)
Exploring the Power of Page Layouts in SharePoint 2010 WCM Sites (http://channel9.msdn.com/Events/TechEd/Europe/2012/OSP335)
Page Layouts in SharePoint (http://blogs.msdn.com/b/kaevans/archive/2011/04/02/page-layouts-in-sharepoint.aspx)
jQuery: ‘jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.’ http://jquery.com/
jQuery Tools: ‘The missing UI library for the Web. jQuery Tools is a collection of the most important user-interface components for modern websites. ‘ http://jquerytools.org/
Recent Comments