首页  >  文章  >  web前端  >  InstantClick兼容MathJax、百度统计等解决办法

InstantClick兼容MathJax、百度统计等解决办法

巴扎黑
巴扎黑原创
2017-09-13 09:35:441178浏览

本篇文章主要介绍了如何让InstantClick兼容MathJax、百度统计等,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

之前有网友提及博客上的LaTex(由MathJax实现)坏掉了,其原因是这里使用了instantclick,以达到网页秒开的效果。但由于instantclick不会重新运行位于head部分的JavaScript代码,而很多工具性软件,比如MathJax、百度统计、Google Analytics、Google Code Prettify等软件都是直接将JS文件插入到head区域。这导致这些工具在instantclick点击后失效,需要重新配置。

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


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

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

以上是InstantClick兼容MathJax、百度统计等解决办法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn