网页一键分享到微信、QQ空间、QQ好友、新浪微博代码

冉冉升起的如银的炊烟,那更古沉默永不停息的小溪,那驮着夕阳缓缓独行的老牛,一方方秧田像棋盘格子,绿绒绒的秧苗,织成一幅幅地毯,远远地伸向天际;丛丛绿树掩映着青砖红瓦的房屋。春天,故乡的松树林依然郁郁苍苍,映山红却已开遍山丘。

通过qq空间、qq聊天、新浪微博和微信二维码分享平台提供的接口,实现把网页中对应的图片、标题、描述的信息参数用javascript获取后传进接口中,实现一键分享。

使用到的接口(测试时需要登录,网址和图片必须是公网的,不能localhost,QQ图片不能太宽,太宽标题描述会undefiend):

1、分享到QQ空间接口

https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=你的网址&sharesource=qzone&title=你的分享标题&pics=你的分享图片&summary=你的分享描述信息

2、分享给QQ好友接口

http://connect.qq.com/widget/shareqq/index.html?url=你的分享网址&sharesource=qzone&title=你的分享标题&pics=你的分享图片地址&summary=你的分享描述&desc=你的分享简述

3、分享到新浪微博接口

http://service.weibo.com/share/share.php?url=你的分享网址&sharesource=weibo&title=你的分享标题&pic=你的分享图片&appkey=你的key需要在新浪微博开放平台中申请

4、分享到微信接口

分享到微信需要将待分享的网页地址转换成二维码,这里推荐使用jQuery-qrcode.js来生成二维码。

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jquery.qrcode.js生成二维码demo</title>
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
  </head>
  <body>
  <div id="ShareToWx"></div>
  <script>
    jQuery(function(){
      jQuery('#ShareToWx').qrcode("https://www.ytymz.com/archives/1050");
    })
  </script>
  </body>
</html>

5、一键分享代码参考如下

<div>分享到:</div> 
<div onclick="shareTo('qzone')">     
    <img src="QQ空间图片地址" width="32">
</div> 
<div onclick="shareTo('qq')">     
    <img src="QQ图片地址" width="32"> 
</div> 
<div onclick="shareTo('sina')">     
    <img src="新浪图片地址" width="32"> 
</div> 
<div onclick="shareTo('wechat')">     
    <img src="微信图片地址" width="32"> 
</div>
function shareTo(types){
 
    var title,imageUrl,url,description,keywords;
 
    //获取文章标题
    title = document.title;
 
    //获取网页中内容的第一张图片地址作为分享图
    //imageUrl = document.images[0].src;
    imageUrl = document.getElementById("pcdetails").getElementsByTagName("img")[0];
    //当内容中没有图片时,设置分享图片为网站logo
    if(typeof imageUrl == 'undefined'){
        imageUrl = 'https://'+window.location.host+'/static/images/logo.png';
    } else {
        imageUrl = imageUrl.src;
    }
 
    //获取当前网页url
    url = document.location.href;
 
    //获取网页描述
    description = document.querySelector('meta[name="description"]').getAttribute('content');
 
    //获取网页关键字
    keywords = document.querySelector('meta[name="keywords"]').getAttribute('content');
 
    //qq空间接口的传参
    if(types=='qzone'){
        window.open('https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+url+'&sharesource=qzone&title="+title+"&pics='+imageUrl+'&summary='+description);
    }
    //新浪微博接口的传参
    if(types=='sina'){
        window.open('http://service.weibo.com/share/share.php?url='+url+'&sharesource=weibo&title="+title+"&pic='+imageUrl+'&appkey=2706825840');
    }
    //qq好友接口的传参
    if(types == 'qq'){
        window.open('http://connect.qq.com/widget/shareqq/index.html?url='+url+'&sharesource=qzone&title="+title+"&pics='+imageUrl+'&summary='+description+'&desc='+keywords);
    }
    //生成二维码给微信扫描分享
    if(types == 'wechat'){
        window.open('二维码生成接口?url='+url);
    }
 
}

以上就是网页一键分享到微信、QQ空间、QQ好友、新浪微博代码。在人生的路上,有一条路每一个人非走不可,那就是年轻时候的弯路。不摔跟头,不碰个头破血流,怎能炼出钢筋铁骨,怎能长大?年轻如你我,是需要经历失败的,只有经历失败。成功的时候,才能体会成功的不易。人生的路上,走得慢一点,看得细一点,走弯路的时候,告诉自己,风景依然美丽,因为,我们没那么赶时间。要记住,每个人都会失败,否则成功的路上将会人满为患。更多关于网页一键分享到微信、QQ空间、QQ好友、新浪微博代码请关注haodaima.com其它相关文章!

标签: 一键 到微信 QQ