如何检查是否加载了Prettify

时间:2013-03-07 05:42:21

标签: javascript google-code-prettify

如果<pre class="prettyprint">存在,我只加载脚本。我发现要启动Prettify,您需要使用prettyPrint();

但是,在没有加载脚本的页面上,我收到Javascript错误。有没有办法测试是否加载了Prettify,然后运行......否则什么都不做?

3 个答案:

答案 0 :(得分:0)

您可以像这样测试:

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

答案 1 :(得分:0)

这应该有效

function getElementsByClassName(className) {
if (document.getElementsByClassName) { 
    return document.getElementsByClassName(className); }
else { return document.querySelectorAll('.' + className); } }

window.onload = function() {
    if (getElementsByClassName('prettyprint').length) {
        /* element exists */
    } else { /* element not exists */ }
}

[编辑]哎呀,你要求存在的功能,我的不好:)

答案 2 :(得分:0)

您不再需要致电prettyPrint

https://code.google.com/p/google-code-prettify/wiki/GettingStarted

  

自动装载机

     

您可以通过一个网址

加载JavaScript和CSS以进行美化
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
     

将加载整个系统并安排美化器在页面加载时运行。您可以指定各种其他选项(作为CGI参数)来配置跑步者。

     

...

相关问题