Using jQuery in Sitecore's Content Editor

Posted Friday, March 19, 2010 2:32 AM by Kevin

It’s spring break and I wasn’t going to make any blog posts this week, but I just ran across this post ( ) from Mark van Aalst concerning using jQuery in Sitecore’s content editor and I just had to comment on it...

I have tried to do this a few times in the past with little to no success. The problem is that, as Mark’s post points out - you need to call $.noConflict() first thing before any other script libraries get loaded. The key that he found in Alexey Rusakov’s FiedTypes module source code (available at by the way) was exactly how to do that... You can insert a custom processor into the renderContentEditor pipeline to output the jQuery script first thing. And the truly brilliant part - Alexey is ensuring it is the very first processor in the pipeline by using the new auto-include config file feature in Sitecore 6! (if you haven’t seen that feature before either, check out on SDN and be amazed)

Mark’s post didn’t 100% put the pieces together for me, so I’ll spell it out here:

Step 1) Create a pipeline processor class to add jQuery to the page’s <head>. Mark’s post has a great example, so I won’t repeat it here.

Step 2) Create a new .config file in /App_Config/Include. The contents just need to look something like this:

<configuration xmlns:x=““>
        <processor x:before=“*[1]” type=“Your.Type,Your.Assembly” />

The magic bit is the before=“*[1]”. This is explained in the SDN link above, but in case you can’t get to that - you can specify any XPath query (relative to the attribute’s node’s parent). *[1] represents the first child, so in this instance we are asking Sitecore to insert our new processor before the existing first child of the <renderContentEditor> element. This ensures it runs first before any other processor has a chance to inject a script into the content editor page.

In the past when trying to get fancy with a custom field type, I’ve had to fall back on using prototype and give up fancy stuff like scriptaculous or jquery while working in the content editor. No more! With this technique, you can always apply jQuery’s $.noConflict() early on and not worry about some other library calling the wrong $() implementation and causing weird errors. Thank you, Mark! And thank you Alexey for sharing your source!


No Comments