js实现QQ、微信、新浪微博分享功能

使用js实现QQ、微信、新浪微博分享功能。

微信分享需要手机扫描二维码,需要对url进行编码。在https协议下,扫描二维码时,浏览器打不开可能是没有安全证书导致的。

js代码:

 1 var shareModel = {
 2 
 3         /**
 4          * 分享QQ好友
 5          * @param  {[type]} title [分享标题]
 6          * @param  {[type]} url   [分享url链接,默认当前页面链接]
 7          * @param  {[type]} pic   [分享图片]
 8          * @return {[type]}       [description]
 9          */
10         shareQQ: function (url, title, pic) {
11             var param = {
12                 url: url || window.location.href,
13                 desc: '', /*分享理由*/
14                 title : title || '', /*分享标题(可选)*/
15                 summary : '',/*分享描述(可选)*/
16                 pics : pic || '',/*分享图片(可选)*/
17                 flash : '', /*视频地址(可选)*/
18                 site: '' /*分享来源 (可选) */
19             };
20             var s = [];
21             for (var i in param) {
22                 s.push(i   '='   encodeURIComponent(param[i] || ''));
23             }
24             var targetUrl = "http://connect.qq.com/widget/shareqq/iframe_index.html?"   s.join('&') ;
25             window.open(targetUrl, 'qq', 'height=520, width=720');
26         },
27 
28         /**
29          * 微信分享
30          * @return {[type]} [description]
31          */
32         weixin: function () {
33             var url = window.location.href,
34                 encodePath = encodeURIComponent(url),
35                 targetUrl = 'http://qr.liantu.com/api.php?text='   encodePath;
36             window.open(targetUrl, 'weixin', 'height=320, width=320');
37         },
38 
39         /**
40          * 分享新浪微博
41          * @param  {[type]} title [分享标题]
42          * @param  {[type]} url   [分享url链接,默认当前页面]
43          * @param  {[type]} pic   [分享图片]
44          * @return {[type]}       [description]
45          */
46         sinaWeiBo: function (title, url, pic) {
47             var param = {
48                 url: url || window.location.href,
49                 type: '3',
50                 count: '1', /** 是否显示分享数,1显示(可选)*/
51                 appkey: '', /** 您申请的应用appkey,显示分享来源(可选)*/
52                 title: '', /** 分享的文字内容(可选,默认为所在页面的title)*/
53                 pic: pic || '', /**分享图片的路径(可选)*/ 
54                 ralateUid:'', /**关联用户的UID,分享微博会@该用户(可选)*/
55                 rnd: new Date().valueOf()
56             }
57             var temp = [];
58             for( var p in param ) {
59                 temp.push(p   '='  encodeURIComponent( param[p ] || '' ) )
60             }
61             var targetUrl = 'http://service.weibo.com/share/share.php?'   temp.join('&');
62             window.open(targetUrl, 'sinaweibo', 'height=430, width=400');
63         }
64     };

 


更多专业前端知识,请上 【猿2048】www.mk2048.com

你可能感兴趣的:(前端)