0

I'm development a blog using django and I have included the 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, but I'm gettin this error in the console: ReferenceError: addthis is not defined.

The addthis code is loaded remotely then, I think that my js is not running because it runs before the addthis script load is complete. How can I fix it?

{% 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 %}
Danna Castro
  • 279
  • 1
  • 4
  • 13

1 Answers1

1

You can wrap your function around window.load like below

$(window).load(function() {
    addthis.sharecounters.getShareCounts('facebook', function(obj) {
        console.log(obj);
    });
})

This will ensure that the code will get executed once entire window is loaded. For further reading read this.

Community
  • 1
  • 1
Tarun Behal
  • 908
  • 6
  • 11