让InstantClick兼容多说、百度统计、百度分享丶MathJax等
使用instantclick会大大提高网页加载速度但不会重新运行带有data-no-instant标记的JavaScript代码,而很多工具性软件,比如多说、百度分享、百度统计、MathJax、Google Analytics、Google Code Prettify等工具会在instantclick点击后失效,需要重新配置。
配置并不复杂,这些工具本身的代码不用做任何修改,该怎么放还怎么放。但在InstantClick.init();之前添加以下代码,部分代码来源于:阅微堂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <script data-no-instant>InstantClick.on('change', function(isInitialLoad) {if (isInitialLoad === false) { // 多说 if ($(".ds-thread").length == 1) DUOSHUO.EmbedThread(".ds-thread") // 多说最近评论小工具 DUOSHUO.RecentCommentsWidget(".ds-recent-comments") // 百度分享 window._bd_share_main.init() // 百度统计 _hmt.push(['_trackPageview', location.pathname + location.search]) //MathJax if (typeof MathJax !== 'undefined') MathJax.Hub.Queue(["Typeset",MathJax.Hub]); //google analytics if (typeof ga !== 'undefined') // support google analytics ga('send', 'pageview', location.pathname + location.search); //google code prettify if (typeof prettyPrint !== 'undefined') // support google code prettify prettyPrint();}});InstantClick.init();</script> |
这段代码做了注释不需要的代码可以删除,通过InstantClick载入完网页会重新加载JS代码,通过直接的函数调用来实现多说、多说最近评论小工具、百度分享、百度统计、MathJax、Google Code Prettify、Google Analytics的重新运行。