Prettify prettyPrint() function not loading inside <script> tag

140 views
Skip to first unread message

Jaime de Miguel

unread,
Aug 13, 2016, 1:22:59 PM8/13/16
to js-code-prettifier

Hi, i have posted this question on stackoverflow http://stackoverflow.com/questions/38933764/prettify-prettyprint-function-not-loading-inside-script-tag

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?

Mike Samuel

unread,
Aug 13, 2016, 1:26:39 PM8/13/16
to js-code-prettifier

Onload runs after all tags in the body have been added to the DOM but a script at the start of the body runs before any other tags in the body have been processed so your script is running fine.  It's just running before there's anything to prettify.

Consider using the run_prettify.js script instead.


--
You received this message because you are subscribed to the Google Groups "js-code-prettifier" group.
To unsubscribe from this group and stop receiving emails from it, send an email to js-code-prettifier+unsub...@googlegroups.com.
To post to this group, send email to js-code-prettifier@googlegroups.com.
Visit this group at https://groups.google.com/group/js-code-prettifier.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages