网络编程
位置:首页>> 网络编程>> JavaScript>> 分享到豆瓣、百度空间、新浪微博的js代码

分享到豆瓣、百度空间、新浪微博的js代码

  发布时间:2011-03-17 12:58:00 

标签:分享,js,微博,按钮

在自己的网站主页上增加社会化分享按钮,是有效提高自己网站流量的一种方法。

今天我在无争围棋网上增加了社会化按钮,根据我个人的习惯,我选择了豆瓣、百度空间、新浪微博、人人网、开心网等几个大型sns网站。下面是我的代码:

function ShareCode(server_url, server_icon_url,text){
    var title = encodeURIComponent(document.title.substring(0,76));
    var url = encodeURIComponent(location.href);
    server_url = server_url.replace("{title}",title);
    server_url = server_url.replace("{url}",url);
    return "<a href="\" mce_href="\""javascript:window.open(\'"
    + server_url
    +"',\'_blank\',\'scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes\'); void 0\" style="\" mce_style="\""color:#000000;text-decoration:none;font-size:12px;font-weight:normal\"><SPAN style="\" mce_style="\""PADDING-RIGHT: 2px; PADDING-LEFT: 2px; FONT-SIZE: 12px; PADDING-BOTTOM: 0px; MARGIN-LEFT: 2px; CURSOR: pointer; PADDING-TOP: 5px\"><IMG alt="
    + text + " src="\" mce_src="\"""
    + server_icon_url
    + "\" align=absMiddle border=0>&nbsp;"
    + text + "<\/SPAN><\/a>"
}
function WriteSNS()
{
    document.writeln("<div id=\"socialbookmark\">");
   
    document.writeln(ShareCode("http://www.douban.com/recommend/?url={url}&title={title}",
    "http://t.douban.com/favicon.ico",
    "推荐到豆瓣"));
   
    document.writeln(ShareCode("http://apps.hi.baidu.com/share/?title={title}&url={url}",
    "http://www.baidu.com/favicon.ico",
    "转帖到百度空间"));
   
    document.writeln(ShareCode("http://v.t.sina.com.cn/share/share.php?title={title}&url={url}",
    "http://t.sina.com.cn/favicon.ico",
    "转发到新浪微博"));
   
    document.writeln(ShareCode("http://www.kaixin001.com/repaste/share.php?rtitle={title}&rurl={url}",
    "http://img1.kaixin001.com.cn/i/favicon.ico",
    "转贴到开心网"));
   
    document.writeln(ShareCode("http://share.renren.com/share/buttonshare.do?title={title}&link={url}",
    "http://s.xnimg.cn\/favicon-rr.ico",
    "转帖到人人网"));
   
    document.writeln("</div>");
};

上面后台函数,一般放在.js文件中。在页面上需要添加分享按钮的地方,调用一次WriteSNS()函数,分享功能即大功告成:

<script type="text/javascript">WriteSNS();</script>

页面效果可以在无争围棋网的对局页上看到,例如:http://nostrive.appspot.com/g2635

这种方法的思路来自月光博客,主要改进是增加了ShareCode函数。将来需要再增加新的sns网站时,只需要增加一个ShareCode函数调用即可,非常简单!

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/skymountain/archive/2010/06/25/5694697.aspx

 

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com