I'm using Googles prettify.js and prettify.css files hosting them myself. This is a javascript and css script that highlights source code snippets inside <pre>
or <code>
tags. Doc can be found here: https://github.com/google/code-prettify/blob/master/docs/getting_started.md
To initialize the script, it can to be called like this: <body onload="prettyPrint();">
which works fine.
I don't want to add this attribute to all the <body>
tags of my pages, instead i want to call it from a <script>
tag inside the <body>
, (for reasons a bit long to explain). But this doesn't work.
Here is the code:
<body>
<script type="text/javascript">
prettyPrint();
//document.write('doing something');//this line prints
</script>
...
The code snippet is not getting highlighted as it did when i called the function through <body onload="prettyPrint();">
but there is no error on the JavaScript console, and the line 'doing something'
is printing.
How can i call this function from the <script>
tag?
EDIT
Even this is printing but still the syntax is not getting highlighted:
<body>
<script type="text/javascript">
if(window.prettyPrint){
prettyPrint();
document.write('doing something');//this line prints
}
</script>
...
So, it has to be called using object.onload
i don't really know why i just tried a lot of things and this worked. Actually it makes a lot of sense anyways.
<body>
<script>
window.onload=function(){prettyPrint();};
</script>
...