views:

337

answers:

2

Im trying to get pretty.js to prettify code in CODE tags, using this js:

onload_functions.push(function() {
    var node_list=document.getElementsByTagName('code');
    for (i=0; i < node_list.length; i++) {
     node_list[i].setAttribute('class','prettyprint');
    }
    prettyPrint();
});

This works fine for Firefox :) but IE's having none of it. What am i doing wrong?

You can see it (non)working at http://sam.xnet.tk

A: 

If the code element has any other class names, this will yield chaos in IE. Using the (IE-specific) JavaScript property className will likely work. I commented on the onload function on your website. Btw, the way you embed the JavaScript it will break future standard-compatible browsers, see the W3C validator's output for your site. The easiest solution is to move the code to an external file.

phihag
Yep, className is IE only. Standards-compliant browsers use class.
Steve Losh
+2  A: 

This would be far simple and work in all browsers with jQuery.

$(function() {
    $('code').addClass('prettyprint');
    prettyPrint();
});

EDIT: The reason it's not working in IE is because IE uses 'className' instead of 'class' just to make life miserable.

Steve Losh
I think i may have to use jquery, was holding off because its another 50Kb people must download to view the page.
Sam
Hm, according to the jQuery site it's only 15kb. Also take a look at this article: http://encosia.com/2008/12/10/3-reasons-why-you-should-let-google-host-jquery-for-you/If you let google host it, then a user won't need to download it if they've ever visited a site that did the same.
Steve Losh