April 6, 2011

jQuery: Highlight Source Code in HTML and ASP.NET Pages




I recently stumbled upon the Snippet plugin which is a syntax highlighting plugin and provides a quick and easy way of highlighting source code passages in HTML documents. I needed a similar implementation for displaying source code (with syntax highlighting and copy), while generating some documentation for a product.

Let us see how to implement this plugin in an ASP.NET page. You can easily replicate this on a HTML page at your end.

Download the snippet.min.js and snippet.min.css and keep it in the Scripts and CSS folder respectively. Then reference these files in your aspx/html page, along with a reference to the jQuery file, as shown below:

jQuery Highlight code

Now declare a <pre> element in your page which contains the source code. I have specified a ‘code’ class for this element. You can call it anything you want:

jquery syntax highlight

The final step is to call Snippet on this <pre> element, as shown below

jQuery Snippet highlight

That’s it. Browse the page and check the syntax highlighted code, as shown below:

image

I have used the “csharp” language with a “bright” style. There are several other languages and parameters supported, as shown below.

image

You can also provide clipboard support to the code by using the ZeroClipboard file. Find more details on implementing the clipboard functionality over here


Giving me +1 tells me you liked this article! Thanks in advance




About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

3 Responses to "jQuery: Highlight Source Code in HTML and ASP.NET Pages"
  1. Beben Koben said...
    April 6, 2011 at 6:01 AM

    hmmm...i think broken link for snippet.min.js
    pliss fixed my friend...hihihi

  2. Anonymous said...
    April 6, 2011 at 7:05 AM

    this small thing my chrome always do whenever i need to use syntax highlighting.

    just open the js file in chrome and they work fine.

    well who need this feature only programmer why they not use alex syntax hightlighing whenever they really want to use syntax highlighting

    this post show that most of garbage [useless] stuff you produce in the thing of got something new [no problem that it is useless]

  3. Suprotim Agarwal said...
    April 6, 2011 at 7:58 AM

    Beben: Thanks! Links fixed :)

    Anonymous: As mentioned in my post, this plugin was used to show code with color syntax, in a documentation created for a product. Users are free to explore alternative ways of using this plugin.

 

Copyright © 2009-2013 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions