Search code examples
javascriptgoogle-code-prettify

how to check if Prettify is loaded


I'm only loading the script if <pre class="prettyprint"> exists. I found that to initiate Prettify, you need to use prettyPrint();.

However, on pages that don't load the script, I get a Javascript error. Is there a way to test if Prettify is loaded, then run... else do nothing?


Solution

  • You can test for it like this:

    if (window.prettyPrint) {
        prettyPrint();
    }