当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 详解如何让InstantClick兼容MathJax、百度统计等

详解如何让InstantClick兼容MathJax、百度统计等

2017年12月12日  | 移动技术网IT编程  | 我要评论

之前有网友提及博客上的latex(由mathjax实现)坏掉了,其原因是这里使用了instantclick,以达到网页秒开的效果。但由于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的重新运行。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网