简体   繁体   English

与柜台分享社交媒体上的网页

[英]Share webpages on social media with counter

I'm creating a website that's going to have hundreds of pages. 我正在创建一个拥有数百个页面的网站。 I want each page to be shareable on Facebook and Twitter . 我希望每个页面都可以在FacebookTwitter上共享。 I've already created these buttons but I also want to have their respective share counters next to my share buttons. 我已经创建了这些按钮,但我也想在共享按钮旁边设置它们各自的共享计数器。 I don't want to use the standard Facebook method they provide because the coding looks bloated. 我不想使用它们提供的标准Facebook方法,因为编码看起来很肿。

Right, so after doing some research, I found this example on codepen . 是的,因此,在进行了一些研究之后,我在codepen上找到了这个示例

This looks exactly what I want - very simple! 这正是我想要的-非常简单!

However, I need some clarification and basic help with how this javascript code works: 但是,对于此javascript代码的工作方式,我需要一些说明和基本帮助:

var permalink = 'http://codepen.io';

var getTwitterCount = function () {
$.getJSON('http://urls.api.twitter.com/1/urls/count.json? 
url='+permalink+'&callback=?', function(data){
var twitterShares = data.count;
$('.twitter .share-count').text(twitterShares);
});
};

getTwitterCount();


var getFacebookCount = function () {
$.getJSON('http://graph.facebook.com/?ids='+permalink+'&callback=?',     
function(data){
var facebookShares = data[permalink].shares;
$('.facebook .share-count').text(facebookShares);
});
};

getFacebookCount();

This bit of code: 这段代码:

var permalink = 'http://codepen.io';

Does this have to be: 这是否必须是:

1) the url of the actual page I want shared, eg: http://www.example.com/page-1/ 1)我要共享的实际页面的网址,例如: http : //www.example.com/page-1/

OR 要么

2) Must this be the root of the domain name, eg: http://www.example.com/ 2)这必须是域名的根,例如: http : //www.example.com/

?

Or am I missing something else? 还是我想念其他东西?

If the answer is #1 above, then that means I have to include + edit this line for each page which isn't ideal because I have all my javascript code + plugins in ONE .js file to reduce http requests, so I'd prefer it that I don't have to add this javascript on-page for every page. 如果答案是上面的#1,则意味着我必须为每个页面添加+编辑此行,这并不理想,因为我将所有javascript代码+插件都放在一个.js文件中以减少http请求,所以我想希望我不必在每个页面上都添加此javascript页面。

那将是您要共享的页面,但是您可以通过将其设置为诸如document.location.href类的document.location.href而不必为每个页面使用单独的变量来解决它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM