首页 > 代码库 > JavaScript,简单的社交圈分享

JavaScript,简单的社交圈分享

var share_templates = {
    //qzone-param    url, title, desc, summary, site
    qzone: ‘http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?‘,
    //qq-param    url, title, desc, pics, source
    qq: ‘http://connect.qq.com/widget/shareqq/index.html?‘,
    //tencent-param    url, title, pic
    tencent: ‘http://share.v.t.qq.com/index.php?c=share&a=index&‘,
    //weibo-param    url, title, pic, appkey
    weibo: ‘http://service.weibo.com/share/share.php?‘,
    //douban-param    href, name, text, image
    douban: ‘http://shuo.douban.com/!service/share?starid=0&aid=0&style=11&‘,
    //linkedin-param    url, title, summary, source
    linkedin: ‘http://www.linkedin.com/shareArticle?mini=true&ro=true&armin=armin&‘,
    //facebook-param    u
    facebook: ‘https://www.facebook.com/sharer/sharer.php?‘,
    //twitter-param    text, url, via
    twitter: ‘https://twitter.com/intent/tweet?‘,
    //google-param    url
    google: ‘https://plus.google.com/share?‘,
    //line      url
    line: ‘https://lineit.line.me/share/ui?‘
};
var share = function(sitename, param){
  var paramString = share_templates[sitename] || share_templates.sitename;
  if(paramString.trim() != ‘‘){
    for (var key in param) {
      if (param.hasOwnProperty(key)) {
        paramString = paramString + key + ‘=‘ + encodeURIComponent(param[key]) + ‘&‘;
      }
    }
    window.open(paramString,‘_blank‘);
  }
}

没有微信的。github上有个不错的项目 https://github.com/overtrue/share.js  就是有点太完美了,如果需求比较简单,可以直接用上面的代码。

顺带一提,分享连接时 param[key] 最好是Unicode编码。

JavaScript,简单的社交圈分享