Jsonp to get the number of Twitter and Facebook articles in concrete steps _javascript Tips

Source: Internet
Author: User
The original is used by Twitter and Facebook, because the domestic is strong, so I feel useful is inside a get JSONP that tool class.

My headache is that many popular APIs already require authentication to get information.

Now that I can access these pages and get the information, why don't I use some simple code to get and skip the verification step?
I think Twitter and Facebook require authentication to get the number of articles, but it turns out that you can get that information through JSONP. Please refer to the steps below.

The JavaScript

I will use basic JavaScript to tell you how to do this:
Copy Code code as follows:

Gets the encapsulated object for the number of articles
var Socialgetter = (function () {
/* JSONP: Get the tool function of the script * *
function Injectscript (URL) {
var script = document.createelement (' script ');
Script.async = true;
script.src = URL;
Document.body.appendChild (script);
}

return {
Getfacebookcount:function (URL, callbackname) {
Injectscript (' https://graph.facebook.com/?id= ' + URL + ' &callback= ' + callbackname);
},
Gettwittercount:function (URL, callbackname) {
Injectscript (' http://urls.api.twitter.com/1/urls/count.json?url= ' + URL + ' &callback= ' + callbackname);
}
};
})();

callback method
function Twittercallback (Result) {
Result.count && Console.log (' The Count is: ', result.count);
}
function Facebookcallback (Result) {
Result.shares && Console.log (' The Count is: ', result.shares);
}

Call
Socialgetter.getfacebookcount (' Http://davidwalsh.name/twitter-facebook-jsonp ', ' facebookcallback ');
Socialgetter.gettwittercount (' Http://davidwalsh.name/twitter-facebook-jsonp ', ' twittercallback ');

Because there are many lightweight micro-frameworks to handle JSONP, the most important part of this article may be the URL to get the information. Choose your JSONP tools according to your needs and habits!

Twitter and Facebook are definitely limited in number and frequency for these requests, so if your site has a lot of traffic, the JSONP is likely to be blocked or blocked. A quick solution is to store the article quantity information in Sessionstorage, but this is only a way for individual users. If you run a large site traffic, you'd better choose to crawl the data on the server side and cache it, and automatically refresh within a certain amount of time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.