I\'m trying to prettify my code in Blogger. I\'ve linked Google JS and CSS files to my template. The problem is that I want the code to be prettified on page load, so I add
There can be a few problems with your snippets above.
If you hook an event with other javascript codes could overwrite it.
Dom.addEventListener
will not work with internet explorer, you need to use Dom.attachEvent
there.
Basicly when I don't or can't use a javascript framework, like jquery for event handling, then I use my old code for onload event handling:
// window.ready
function register_onload(func){
var oldOnload = window.onload;
if(typeof(oldOnload) == 'function'){
window.onload = function(){
oldOnload();
func();
}
}else{
window.onload = function(){
func();
}
}
}
// document.ready
function register_onload(func){
var old_onload = document.onreadystatechange;
if(typeof old_onload == 'function'){
document.onreadystatechange = function(){
old_onload();
if(document.readyState == 'complete'){
func();
}
}
}else{
document.onreadystatechange = function(){
if(document.readyState == 'complete'){
func();
}
}
}
}
I attach two codes which work in slightly different ways. Mainly if you use document.ready( 2nd snippet ), then the hooked code will run when the page is loaded and all HTML elements are created. But on document.ready, there is no guarantee, that all javasript and css codes are loaded also, so if you want all codes and scripts to be loaded, then you need window.ready( 1st snippet ).
For your situation, I belive you need the 2nd, document.ready snippet and you can use it the following way:
// copy 2nd snippet here...
register_onload(function(){
prettyPrint();
});
Also I recommend you put your javacript code between /**/
symbols, so your final code will look like:
Hope this helps you solve your problem.