我有一个索引页面,其中 Pinterest 按钮对于不同的项目会多次可见。 我想确保页面加载速度快,并且 Pinterest 按钮不会减慢页面速度。
然而,由于 pinterest JS 解析 DOM 并替换 pinit 按钮中的代码,如果按钮数量很多,这对我来说会变得昂贵。
我可以以某种方式改进这个过程吗?如何仅在需要时调用 pinterest 脚本?
最佳答案
以下是我们对想要异步加载 Pinterest 脚本的网站运营商的建议:
<script type="text/javascript">
(function(d){
var f = d.getElementsByTagName('SCRIPT')[0], p = d.createElement('SCRIPT');
p.type = 'text/javascript';
p.async = true;
p.src = '//assets.pinterest.com/js/pinit.js';
f.parentNode.insertBefore(p, f);
}(document));
</script>
这不应阻止页面加载。
关于pinterest - 需要时加载 pinterest 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356085/