使用谷歌代码Pretify回调

Using callbacks with Google Code Prettify

本文关键字:Pretify 回调 代码 谷歌      更新时间:2023-09-26

我在页面上使用谷歌代码pretify(工作正常(,我想添加一个函数,该函数在这个过程完成后被调用。

在文档中,他们描述了以下参数:

callback=js_ident    window.exports["js_ident"] will be called when prettyprinting finishes. If specified multiple times, all are called.

然而,我还没能做到这一点。很明显,我错过了回调函数应该如何定义/导出的一些内容。

我的标题看起来是这样的(当页面加载时,代码被正确地美化了,但警报没有显示(:

<script type='text/javascript'>function testing(){alert('hello')}}</script>
<script type='text/javascript' src='https://google-code-prettify.googlecode.com/svn/loader/prettify.js?callback=testing'></script>

此外,在这个例子之后,我尝试用几种不同的方式修改第一个块((下面显示的几个(,但没有改变:

<script type='text/javascript'>window['exports'] = {testing: function(){alert('123')}}</script>
<script type='text/javascript'>window.exports = {testing: {apply: function(){alert('123')}}}</script>

我应该如何定义testing函数,以便正确调用它?

callback参数似乎只在run_prettify.js脚本上可用,而在您当前使用的prettify.js脚本上不可用。

此外,根据文档,他们希望您在callback参数中指定的函数在window.exports对象中指定。

E.ghttp://jsbin.com/atukuq/1/

<script type='text/javascript'>
  window.exports = { 
    testing: function () {
      alert('hello');
    }
  }
</script>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?callback=testing"></script>
<script type='text/javascript'>
   window.exports = [];
   window.exports["testing"] = function() {
      alert("hello");
   }
</script>
<script type='text/javascript' src='https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?callback=testing'></script>

更改:用run_prettify.js代替prettify.js,并根据文档定义函数。