Client-Side code highlighting

When I visited Alex Gorbatchev’s blog, I noticed he had a unique client-side code highlighting solution in place, one I hadn’t seen anywhere else. That’s something I’ve wanted on my blog for a while; the vanilla <PRE> sections I’ve been using are serviceable, but primitive. Although I loves me some Regex, I don’t know squat about PERL so I’ve been hesitant to hack anything fancier into my Movable Type install.

After I asked Alex about his solution, he was nice enough to package up his JavaScript syntax highlighter as dp.SyntaxHighlighter. It’s really quite slick, and it works with all kinds of code: C#, VB.NET, JavaScript, PHP, SQL, and XML. Here’s a VB.NET sample:

Public Function GetGoogleWordFrequency(ByVal strWord As String) As Integer
Dim strUrl As String = "http://www.google.com/search?num=1&q=" & _
Web.HttpUtility.UrlEncode(strWord)
Dim wc As New WebClientGzip
Dim strPageContents As String = wc.DownloadDataGzip(strUrl)
Dim m As Match
m = Regex.Match(strPageContents, _
"results.*?d+.*?d+.*?of about.*?(?[d,]+)", _
RegexOptions.IgnoreCase)
If Not m Is Nothing Then
Dim strResults As String = m.Groups("TotalResults").ToString
If strResults.Length > 0 Then
Return Convert.ToInt32(strResults.Replace(",", ""))
End If
End If
Return -1
End Function

Alex tested this JavaScript on FireFox and IE6, and is looking for compatibility feedback on other browsers.

Related posts

Secrets of the JavaScript Ninjas

One of the early technology decisions we made on Stack Overflow was to go with a fairly JavaScript intensive site. Like many programmers, I've been historically ambivalent about JavaScript: * The Power of "View Source" * The Day Performance Didn't Matter Any More * JavaScript and HTML:

By Jeff Atwood ·
Comments

The Great Browser JavaScript Showdown

In The Day Performance Didn't Matter Any More, I found that the performance of JavaScript improved a hundredfold between 1996 and 2006. If Web 2.0 is built on a backbone of JavaScript, it's largely possible only because of those crucial Moore's Law performance

By Jeff Atwood ·
Comments

JavaScript: The Lingua Franca of the Web

Mike Shaver, a founding member of the Mozilla team, has strong feelings about how the web became popular: If you choose a platform that needs tools, if you give up the viral soft collaboration of View Source and copy-and-paste mashups and being able to jam jQuery in the hole that

By Jeff Atwood ·
Comments

JavaScript and HTML: Forgiveness by Default

I’ve been troubleshooting a bit of JavaScript lately, so I’ve enabled script debugging in IE7. Whenever the browser encounters a JavaScript error on a web page, instead of the default, unobtrusive little status bar notification... ... I now get one of these glaring, modal error debug notification dialogs: I

By Jeff Atwood ·
Comments

Recent Posts

Let's Talk About The American Dream

Let's Talk About The American Dream

A few months ago I wrote about what it means to stay gold — to hold on to the best parts of ourselves, our communities, and the American Dream itself. But staying gold isn’t passive. It takes work. It takes action. It takes hard conversations that ask us to confront

By Jeff Atwood ·
Comments
Stay Gold, America

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood ·
Comments
The Great Filter Comes For Us All

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven’t any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don’t stop there – read the Story of Your Life novella it was based on for so much

By Jeff Atwood ·
Comments
I Fight For The Users

I Fight For The Users

If you haven’t been able to keep up with my blistering pace of one blog post per year, I don’t blame you. There’s a lot going on right now. It’s a busy time. But let’s pause and take a moment to celebrate that Elon Musk

By Jeff Atwood ·
Comments