<?xml version="1.0"?>
<rss version="2.0"><channel><title>Devplace</title><link>http://www.devplace.nl/blog</link><lastBuildDate>Sat, 26 Dec 09 10:54:52 +0100</lastBuildDate><generator>Habari 0.7-alpha http://habariproject.org/</generator><item><title>Loading SyntaxHighlighter on demand</title><link>http://www.devplace.nl/blog/loading-syntaxhighlighter-on-demand</link><description>SyntaxHighlighter is a great JavaScript script, but it lacks one important feature in my opinion.&#xD;
&#xD;
For this blog, I needed a syntax highlighter for the code examples that will be shown here. I soon found &lt;a href="http://alexgorbatchev.com/wiki/SyntaxHighlighter"&gt;SyntaxHighlighter&lt;/a&gt; as I was looking for an unobtrusive JavaScript solution. But there's one thing that I didn't like about it. You need to load all JavaScript files in advance even if you don't need syntax highlighting on a given page. This is not a problem when you're building static pages, because then you can just include the needed JavaScript files yourself. For this blog however I wanted SyntaxHighlighter to be loaded on demand without specifying for which page the SyntaxHighlighter should be loaded.&#xD;
&#xD;
So I came up with the nifty JavaScript solution below. It uses &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; because jQuery is already loaded on all pages of this blog. But it shouldn't be so hard to do the same without jQuery.&#xD;
&#xD;
&lt;pre class="brush: javascript"&gt;&#xD;
$(document).ready(&#xD;
	function() {&#xD;
&#xD;
		// only load syntax hilighter when needed&#xD;
		// So check for any HTML &amp;lt;pre&amp;gt; elements for which the class attribute starts with 'brush'&#xD;
		if ($(&amp;quot;pre[class^=brush]&amp;quot;).length &amp;gt; 0) {&#xD;
	&#xD;
			var jsPath = themeUrl + '/js/hilighter/';&#xD;
			var cssPath = themeUrl + '/css/hilighter/';&#xD;
	&#xD;
			// Defines which languages should be supported&#xD;
			var brushes = ['Bash','Php','Plain','JScript', 'Sql'];&#xD;
	&#xD;
			var jsBrushes = '';&#xD;
			for (var i = 0; i &amp;lt; brushes.length; i) {&#xD;
				jsBrushes  = '&amp;lt;script src=&amp;quot;' + jsPath + 'shBrush' + brushes[i] + '.js&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;';&#xD;
			}&#xD;
	&#xD;
			// Append SyntaxHighlighter scripts to the HTML head&#xD;
			$(&amp;quot;head&amp;quot;).append(&#xD;
				'&amp;lt;script src=&amp;quot;' + jsPath + 'shCore.js&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;' +&#xD;
				  jsBrushes +&#xD;
				  '&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;' + cssPath + 'shCore.css&amp;quot; type=&amp;quot;text/css&amp;quot;&amp;gt;' +&#xD;
				  '&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;' + cssPath + 'shThemeDefault.css&amp;quot; type=&amp;quot;text/css&amp;quot;&amp;gt;'&#xD;
			);&#xD;
			// Apply SyntaxHilighlighter&#xD;
			SyntaxHighlighter.all();&#xD;
		}&#xD;
	}&#xD;
);&#xD;
&lt;/pre&gt;&#xD;
&#xD;
I think the script is pretty much self-explanatory. First we check if there are one or more PRE-elements having a classname that starts with "brush". If one or more of such PRE-elements exist in our HTML code, the appropriate SyntaxHighlighter CSS and JavaScript files will be loaded by adding script tags to the HTML head.</description><pubDate>Tue, 01 Sep 09 22:12:07 +0200</pubDate><guid isPermaLink="false">tag:devplace.vandesande.no-ip.org,2009:loading-syntaxhighlighter-on-demand/1251835910</guid></item></channel></rss>

