最近频繁的使用将内容分享到各大社交平台的功能,因此对该功能做了一下小小的总结,主要是使用Javascript将内容分享到各大社交平台网站进行归纳整理了一下,小结要点如下,希望能帮到有需要的朋友。
关键性的代码(Javascript):
function sharePlugin(name,img,title){ var purl=encodeURIComponent(document.location.href); switch(name){ case 'sina': var url='http://service.t.sina.com.cn/share/share.php?url='+purl+'&appkey=2858164115&title='+encodeURIComponent(""+title)+'&pic='+encodeURIComponent(img)+'&ralateUid=&searchPic=false';//注意这个地方使用了一个appkey,分享到新浪的时候会有一个分享来源,其中的分享来源就取决于这个appkey,你可以根据需要改成自己的appkey window.open(url); break; case 'tqq': var url='http://v.t.qq.com/share/share.php?title='+encodeURIComponent(""+title)+'&url='+purl+'&appkey='+encodeURI("8675d8896e054316bc69755118dea3c9")+'&site='+purl+'&pic='+img; window.open( url,'转播到腾讯微博','width=700,height=680,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,location=yes,resizable=no,status=no' ); break; case 'kaixin': var url='http://www.kaixin001.com/repaste/share.php?rurl='+purl+'&rcontent='+encodeURIComponent(""+title)+'&rtitle='+encodeURIComponent("欣和食与家"); window.open(url); break; case 'renren': var url='http://share.renren.com/share/buttonshare.do?link='+purl+'&title='+encodeURIComponent(""+title); window.open(url); break; case 'douban': var url='http://www.douban.com/recommend/?url='+purl+'&title='+encodeURIComponent(""+title); window.open(url); break; case 'qzone': var url='http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+encodeURIComponent(document.location.href)+'&title='+encodeURIComponent(""+title)+'&pics='+img; window.open(url); break; } }
以上代码经净化处理后:
function sharePlugin(name,img,title){var purl=encodeURIComponent(document.location.href);switch(name){case'sina':var url='http://service.t.sina.com.cn/share/share.php?url='+purl+'&appkey=2858164115&title='+encodeURIComponent(""+title)+'&pic='+encodeURIComponent(img)+'&ralateUid=&searchPic=false';window.open(url);break;case'tqq':var url='http://v.t.qq.com/share/share.php?title='+encodeURIComponent(""+title)+'&url='+purl+'&appkey='+encodeURI("8675d8896e054316bc69755118dea3c9")+'&site='+purl+'&pic='+img;window.open(url,'转播到腾讯微博','width=700,height=680,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,location=yes,resizable=no,status=no');break;case'kaixin':var url='http://www.kaixin001.com/repaste/share.php?rurl='+purl+'&rcontent='+encodeURIComponent(""+title)+'&rtitle='+encodeURIComponent("欣和食与家");window.open(url);break;case'renren':var url='http://share.renren.com/share/buttonshare.do?link='+purl+'&title='+encodeURIComponent(""+title);window.open(url);break;case'douban':var url='http://www.douban.com/recommend/?url='+purl+'&title='+encodeURIComponent(""+title);window.open(url);break;case'qzone':var url='http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+encodeURIComponent(document.location.href)+'&title='+encodeURIComponent(""+title)+'&pics='+img;window.open(url);break}}
具体的使用方法如下(完整代码):
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>php.hhsy.cc</title> <script language="javascript"> function sharePlugin(name,img,title){var purl=encodeURIComponent(document.location.href);switch(name){case'sina':var url='http://service.t.sina.com.cn/share/share.php?url='+purl+'&appkey=2858164115&title='+encodeURIComponent(""+title)+'&pic='+encodeURIComponent(img)+'&ralateUid=&searchPic=false';window.open(url);break;case'tqq':var url='http://v.t.qq.com/share/share.php?title='+encodeURIComponent(""+title)+'&url='+purl+'&appkey='+encodeURI("8675d8896e054316bc69755118dea3c9")+'&site='+purl+'&pic='+img;window.open(url,'转播到腾讯微博','width=700,height=680,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,location=yes,resizable=no,status=no');break;case'kaixin':var url='http://www.kaixin001.com/repaste/share.php?rurl='+purl+'&rcontent='+encodeURIComponent(""+title)+'&rtitle='+encodeURIComponent("欣和食与家");window.open(url);break;case'renren':var url='http://share.renren.com/share/buttonshare.do?link='+purl+'&title='+encodeURIComponent(""+title);window.open(url);break;case'douban':var url='http://www.douban.com/recommend/?url='+purl+'&title='+encodeURIComponent(""+title);window.open(url);break;case'qzone':var url='http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+encodeURIComponent(document.location.href)+'&title='+encodeURIComponent(""+title)+'&pics='+img;window.open(url);break}} </script> </head> <body> <a href="#" onclick="sharePlugin('renren','http://php.hhsy.cc/images/logo.gif','http://php.hhsy.cc')">分享到人人网</a> <br /> <a href="#" onclick="sharePlugin('tqq','http://php.hhsy.cc/images/logo.gif','http://php.hhsy.cc')">分享到腾讯微博</a> <br /> <a href="#" onclick="sharePlugin('sina','http://php.hhsy.cc/images/logo.gif','http://php.hhsy.cc')">分享到新浪微博</a> <br /> <a href="#" onclick="sharePlugin('kaixin','http://php.hhsy.cc/images/logo.gif','http://php.hhsy.cc')">分享到开心网</a> </body> </html>