如何正确加载addthis widget js?
how to load correctly addthis widget js?
我正在使用 django 开发一个博客,我已经包含了 addthis tool for include the share buttons. I'm including the addthis buttons in the posts detail page. I need to get the facebook shares count for the current blog post deetail. I'm using these steps,但是我在控制台中遇到了这个错误:ReferenceError: addthis is not defined.
addthis 代码是远程加载的,我认为我的 js 不是 运行 因为它在 addthis 脚本加载完成之前运行。我该如何解决?
{% block js %}
<script type="text/javascript"src="//s7.addthis.com/js/300/addthis_widget.js#pubid=fdfs" async="async"></script>
<script src="{% static 'js/blog/blog_list.js' %}"></script>
<script>
$(function () {
addthis.sharecounters.getShareCounts('facebook', function(obj) {
console.log(obj);
});
})
</script>
{% endblock %}
您可以将您的函数包裹在 window.load 周围,如下所示
$(window).load(function() {
addthis.sharecounters.getShareCounts('facebook', function(obj) {
console.log(obj);
});
})
这将确保在加载整个 window 后执行代码。如需进一步阅读,请阅读 this.
我正在使用 django 开发一个博客,我已经包含了 addthis tool for include the share buttons. I'm including the addthis buttons in the posts detail page. I need to get the facebook shares count for the current blog post deetail. I'm using these steps,但是我在控制台中遇到了这个错误:ReferenceError: addthis is not defined.
addthis 代码是远程加载的,我认为我的 js 不是 运行 因为它在 addthis 脚本加载完成之前运行。我该如何解决?
{% block js %}
<script type="text/javascript"src="//s7.addthis.com/js/300/addthis_widget.js#pubid=fdfs" async="async"></script>
<script src="{% static 'js/blog/blog_list.js' %}"></script>
<script>
$(function () {
addthis.sharecounters.getShareCounts('facebook', function(obj) {
console.log(obj);
});
})
</script>
{% endblock %}
您可以将您的函数包裹在 window.load 周围,如下所示
$(window).load(function() {
addthis.sharecounters.getShareCounts('facebook', function(obj) {
console.log(obj);
});
})
这将确保在加载整个 window 后执行代码。如需进一步阅读,请阅读 this.