|
jQuery Getting Started with jQuery and SharePointInstalling jQuery into your FarmFirst things first you need to find a smart way to get jQuery reference into your pages. By far the neatest way is Jan Tielens Smart Tools Adding Your Own ScriptsIf you're doing AJAX calls, you want to load the jQuery library as soon as possible so your $.ajax (and helper method) calls can start loading while the page renders. If you're using the *AdditionalPageHead Delegate Control You also want to load other scripts asynchoronously. Always using < SCRIPT > tags will cause each script to block the next. A better approach is to use a $.getScript() call or dynamically create the < SCRIPT > tag and append it to the HEAD. jQuery's $.getScript() method appends a random query string parameter to the request which prevents the browser from using the cache. For most scripts, I prefer dynamic < SCRIPT > tags because you more frequently use the cache (304 NOT MODIFIED). To achieve this, your second < SCRIPT > tag in the HEAD is the loader. The loader should load JavaScript or related CSS in parallel with the remaining SharePoint script load (e.g., core.js). Here's an example of a loader script: <script type="text/javascript" src="/javascripts/jquery.min.js"></script> <script type="text/javascript"> /* * includeScript plugin * Copyright (c) 2009 Paul Grenier (endusersharepoint.com) * Licensed under the MIT (MIT-LICENSE.txt) */ (function($){ var _debug = function(msg,type){ if (window.console && window.console.log){ if (!type){type="log";} console[type](msg); return true; } return false; }; $.fn.includeFile = function(options, callback){ var defaults = {"tag":"script", "url":"", "media":"screen", "rel":"stylesheet" }, o = $.extend({},defaults,options), head = this[0], file = document.createElement(o.tag); switch (o.tag){ case "script": file.src = o.url; file.type = o.type?o.type:"text/javascript"; if (o.charset){file.charset=o.charset;} if (o.defer){file.defer="defer";} break; case "link": file.type = o.type?o.type:"text/css"; file.rel = "stylesheet"; file.href = o.url; file.media = "screen"; if (o.target){file.target=o.target;} if (o.rev){file.rev=o.rev;} if (o.hreflang){file.hreflang=o.hreflang;} if (o.charset){file.charset=o.charset;} if (callback){_debug("not all browsers support callback for <"+o.tag+">","warn");} break; default: _debug("unknown tag: <"+o.tag+">","error"); return $(this); } file.onload = file.onreadystatechange = function(){ if (!this.readyState || this.readyState == "loaded" || this.readyState == "complete"){ if (callback && typeof callback=="function"){callback(o);} file.onload = file.onreadystatechange = null; } }; head.appendChild(file); //using the jQuery object and append methods force an xhr return $(this); }; })(jQuery); //end plugin //start instructions $("head:first") .includeFile({"url":"/javascripts/accordion-nav.js"},function(){ $("head:first").includeFile({"tag":"link","url":"/javascripts/leftnav.css"}); //load the css file as a callback so it degrades }) .includeFile({"url":"/javascripts/expand-collapse-all.js"}) //another call chained from the first .includeFile({"url":"/javascripts/ows.js"}); //end with semi-colon //end instructions </script> Another benefit of this pattern is that Firebug can see all of these scripts and classes (not always possible with AJAX loads using the $.getScript or similar methods. However, seeing a returned 304 NOT MODIFIED means that the browser is still requesting the file every time. For further performance gain, you can edit the web.config to return "cacheable" in the response headers for certain file types. This will eliminate the 304 NOT MODIFIED message because the browser does not make a new request. See Optimization, BLOB caching and HTTP 304s Debugging jQuery and SharePointBy far the most recommended approach is a tool called FireBug debugger; If you want to debug with browsers other than FireFox, then you can use the Jquery Debugger Plugin Selecting elements on SharePoint ASPX pages with jQueryIf you're looking at jQuery in SharePoint, $("#...") selector will only return first element because IDs should be unique (however, SP often renders multiple elements with the same ID) and also because of issues with special characters, use $("[id='...']") instead e.g. $("[id='foobar']") instead of $("#foobar"). Finding stuff in the jQuery API@Autosponge Using the Google AJAX Libraries API to load jQueryjQuery can also be loaded from the Google web servers into SharePoint. This method brings the following advantages:
To use the API replace your existing reference to jQuery with the following: <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("jquery", "1.3.2"); </script> See more on the AJAX Libraries API site External References
|
jQuery and SharePoint

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare









Comments (4)
Apr 28, 2009
Mike Hansford says:
I've found one of the main culprits of SharePoint producing multiple elements wi...I've found one of the main culprits of SharePoint producing multiple elements with the same ID is the RSS Viewer Web Part. However, this has been mainly a result of a poorly formed RSS file to begin with. I've come across several sites that put content inside CDATA sections in the element of the RSS file. Inside the CDATA sections are DIV tags with an ID attribute. These attributes are repeated from one entry to another. This is what has caused the invalid HTML markup.
Interestingly, I've also found that Firefox just parses the whole document and finds all the DIV tags. IE takes the view that the rules are the rules and that an ID tag will be unique.
Jun 05, 2009
Anonymous says:
Thanks for the post. Here is a simple blog post about integrating JQuery in Shar...Thanks for the post. Here is a simple blog post about integrating JQuery in SharePoint on a simple page. Hope this helps people who are new to SharePoint.
http://praveenbattula.blogspot.com/2009/05/jquery-integration-in-sharepoint.html
Jun 05, 2009
Paul Grenier says:
That approach is fine for testing, but if you stick with it you'll run into a fe...That approach is fine for testing, but if you stick with it you'll run into a few problems.
First, you'll have multiple copies of jQuery in the environment. As a result, users will not cache or used a cached copy (that's the whole reason to use Google's version if you can).
Second, Once you bind jQuery to the single page in a script tag, you can't add it to the master. If you do, and let's say, the .master has a newer version, the one on the page will overwrite it. It should be done with a lazy load method in a single page, NOT a script tag.
Third, with different versions of jQuery on the site, you can't add ANY custom scripts to the .master because unit testing is a train wreck. Everything becomes a one-off.
I highly recommend working with a single version of jQuery and putting it everywhere (i.e., the .master). Second to that, use a local version of jQuery called jquery-latest.js or something, and pointing all of your pages to that version using a lazy load method. That way, if you later add it to a .master, nothing breaks and it doesn't try to load twice or load different versions. Lastly, when you make a change to your jQuery version, test all of your scripts--this is especially important for SP because our audience is largely IE and IE always seems to log more bugs than other browsers (and they're not all found before a new version releases e.g., the :visible bug).
Nov 05, 2009
Christophe Humbert says:
You can also call the jQuery script directly from the Google or Microsoft CDN, w...You can also call the jQuery script directly from the Google or Microsoft CDN, without having to load the Google API.
@Paul: in practice having a single version of jQuery is not realistic when you rely on third party plugins. You'll come across very useful plugins that are not regularly maintained, and will only work with specific versions of jQuery (or be upgraded with a delay). I have even seen articles that explain how to manage multiple versions of jQuery in the same Web page.