首頁  >  文章  >  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