Navigation menu

Dynamic in-page code highlighter

  1. Put this in the header of your web page:
    <script type="text/JavaScript" src="/parse.js"></script>
  2. Put your code in 'pre' elements with the 'class' attribute set to the name of languages defined in the 'parse.js' script. Make sure they are also recognised by the code highlighting stylesheet.
  3. Put this at the end:
    <script type="text/JavaScript" src="/highlight.js"></script>
    …wherein the content of highlight.js is the following:
    for ( var i = 0 , candidates = document.getElementsByTagName( "pre" ) ;
    candidates[i] ; i++ ) {
    if ( String.prototype.parse[ candidates[i].getAttribute( "class" ).toLowerCase() ] )
    	candidates[i].removeChild( candidates[i].firstChild ).nodeValue.parse(
    				candidates[i].getAttribute( "class" ) ,
    			candidates[i] , "link" ) ;

If you can tell me why this doesn't work with Internet Explorer that would be nice, thank-you.

You can see the source code of this page here.