6

让 instantclick 兼容 MathJax、百度统计等

 3 years ago
source link: https://zhiqiang.org/coding/instantclick-support-mathjax-baidu-stat.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

让 instantclick 兼容 MathJax、百度统计等

作者: 张志强

, 发表于 2014-12-25

, 共 1017 字 , 共阅读 381 次

由于 instantclick 不会重新运行位于 head 部分的 JavaScript 代码,而很多工具性软件,比如 MathJax、百度统计、Google Analytics、Google Code Prettify 等软件都是直接将 JS 文件插入到 head 区域。这导致这些工具在 instantclick 点击后失效,需要重新配置。

配置并不复杂,这些工具本身的代码不用做任何修改。但在InstantClick.init();之前添加以下代码:

<script data-no-instant>
InstantClick.on('change', function(isInitialLoad) {
  if (isInitialLoad === false) {
    if (typeof MathJax !== 'undefined') // support MathJax
      MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
    if (typeof prettyPrint !== 'undefined') // support google code prettify
      prettyPrint();
    if (typeof _hmt !== 'undefined')  // support 百度统计
      _hmt.push(['_trackPageview', location.pathname + location.search]);
    if (typeof ga !== 'undefined')  // support google analytics
        ga('send', 'pageview', location.pathname + location.search);
  }
});
InstantClick.init();
</script>

它的含义是每次页面重载时,通过直接的函数调用来实现 MathJax、百度统计、Google Code Prettify、Google Analytics 的重新运行。

Q. E. D.

avatar-0.jpg

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK