网站首页学无止境JS

微信WeixinJSBridge API 之 朋友圈分享代码实例

发布时间:2014-12-04 11:22:24编辑:songlin阅读(393)

    废话少说直接上实例代码:

    1. <script type="text/javascript"
    2.     var dataForWeixin = { 
    3.         appId: ""
    4.         img: "分享显示的图片"
    5.         url: '分享地址的链接地址'
    6.         title: '分享显示的标题'
    7.         desc: '分享显示的描述'
    8.         fakeid: ""
    9.     }; 
    10.     (function() { 
    11.         var onBridgeReady = function() { 
    12.             // 发送给好友; 
    13.             WeixinJSBridge.on('menu:share:appmessage'function(argv) { 
    14.                 WeixinJSBridge.invoke('sendAppMessage', { 
    15.                     "appid": dataForWeixin.appId, 
    16.                     "img_url": dataForWeixin.img, 
    17.                     "img_width""120"
    18.                     "img_height""120"
    19.                     "link": dataForWeixin.url, 
    20.                     "desc": dataForWeixin.desc, 
    21.                     "title": dataForWeixin.title 
    22.                 }, function(res) { 
    23.                     _hmt.push(['_trackEvent''durexworldcup''share''wechat''friend']); 
    24.                 }); 
    25.             }); 
    26.             // 分享到朋友圈; 
    27.             WeixinJSBridge.on('menu:share:timeline'function(argv) { 
    28.                 WeixinJSBridge.invoke('shareTimeline', { 
    29.                     "img_url": dataForWeixin.img, 
    30.                     "img_width""120"
    31.                     "img_height""120"
    32.                     "link": dataForWeixin.url, 
    33.                     "desc": dataForWeixin.desc, 
    34.                     "title": dataForWeixin.desc 
    35.                 }, function(res) { 
    36.                     _hmt.push(['_trackEvent''durexworldcup''share''wechat''friends']); 
    37.                 }); 
    38.             }); 
    39.         }; 
    40.         if (document.addEventListener) { 
    41.             document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); 
    42.         } else if (document.attachEvent) { 
    43.             document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
    44.             document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); 
    45.         } 
    46.     })(); 
    47. </script> 

    微信朋友圈分享,转发就这么简单。