1. 程式人生 > >使用Javascript 實現 分享到 新浪微博 QQ 空間等

使用Javascript 實現 分享到 新浪微博 QQ 空間等

我們閱讀部落格的時候經常會用到這樣功能,當然有時候也會想把自己的網站上也加入類似的分享功能,各大廠商已經給出了相應的API,點選一個按鈕即可彈出視窗進入分享,我們事先可以設定一些引數,一般常用的就是 網站的網址,圖片還有一些內容描述。這裡我寫了三個方法,分別分享到 新浪微博,Q空間,以及騰訊微博

[javascript] view plaincopy
  1. <span style="font-size:24px;">var ShareTip = function()  
  2. {  
  3. }  
  4. //分享到騰訊微博
  5. ShareTip.prototype.sharetoqq=function
    (content,url,picurl)  
  6. {  
  7.  var shareqqstring='http://v.t.qq.com/share/share.php?title='+content+'&url='+url+'&pic='+picurl;  
  8.  window.open(shareqqstring,'newwindow','height=100,width=100,top=100,left=100');  
  9. }  
  10. //分享到新浪微博
  11. ShareTip.prototype.sharetosina=function(title,url,picurl)  
  12. {  
  13.  var sharesinastring=
    'http://v.t.sina.com.cn/share/share.php?title='+title+'&url='+url+'&content=utf-8&sourceUrl='+url+'&pic='+picurl;  
  14.  window.open(sharesinastring,'newwindow','height=400,width=400,top=100,left=100');  
  15. }  
  16. //分享到QQ空間
  17. ShareTip.prototype.sharetoqqzone=function(title,url,picurl)  
  18. {  
  19.  var shareqqzonestring=
    'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?summary='+title+'&url='+url+'&pics='+picurl;  
  20.  window.open(shareqqzonestring,'newwindow','height=400,width=400,top=100,left=100');  
  21. }  
  22. </span>  

 以上的函式分別具有三個引數,內容,網址,以及 圖片,也就是我們想要分享的內容

測試程式碼如下(我們可以寫在一個按鈕的單擊事件裡,關於按鈕的長相,這個自己定義