Menu

Add this - Get most shared article with jQuery

Updated: June 11, 2012

Ali Taheri

Here's how to share articles on your website and retrieve those which have been shared most often: 

  1. Register on http://www.addthis.com/
  2. Get the code for your website.
  3. In the code you can find the Publisher Profile ID (PubID)
  4. Copy the PubID and replace it with ***** PUT YOUR PubID HERE ***** in line 8 in the code below.
<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            $.ajax({
                // Period can be changed to day or week or month
                url: "http://q.addthis.com/feeds/1.0/shared.json?pubid= ***** PUT YOUR PUB ID HERE ******* &period=month",
                dataType: "jsonp",
                success: function (data, status, xhr) {
                    var message = "<ol>";

                    //in this example the last 5 articles will be shown but you can change it (just change the 4 in the line below)
                    for (i = 0; i < data.length && i < 4; i++) {
                        message += '<li><a href="' + data[i].url + '">' + data[i].title.split("|", 1) + '</a></li>';
                    }
                    message += "</ol>";
                    $('#most-shared-article').html(message);

                },
                error: function (xhr, status, error) {
                    $('#most-shared-article').html('Error occurred: ' + status);
                }
            })

        });

    </script>
</head>
<body>
    <div id="most-shared-article">
    </div>
</body>
</html>

Tags

jQuery AddThis
comments powered by Disqus

© 2017 - Ali Sheikh Taheri