<address id="ttjl9"></address>

      <noframes id="ttjl9"><address id="ttjl9"><nobr id="ttjl9"></nobr></address>
      <form id="ttjl9"></form>
        <em id="ttjl9"><span id="ttjl9"></span></em>
        <address id="ttjl9"></address>

          <noframes id="ttjl9"><form id="ttjl9"></form>

          H5之外部瀏覽器喚起微信分享

          2019-10-19    seo達人

          最近在做一個手機站,要求點擊分享可以直接打開微信分享出去。而不是jiathis,share分享這種的點擊出來二維碼。在網上看了很多,都說APP能喚起微信,手機網頁實現不了。也找了很多都不能直接喚起微信。

          總結出來一個可以直接喚起微信的。適應手機qq瀏覽器和uc瀏覽器。

          下面上代碼,把這些直接放到要轉發的頁面里就可以了:

          html部分:

          
                  
          1. <script src="mshare.js"></script>//引進mshare.js
          2. <button data-mshare="0">點擊彈出原生分享面板</button>
          3. <button data-mshare="1">點擊觸發朋友圈分享</button>
          4. <button data-mshare="2">點擊觸發發送給微信朋友</button>

          js部分:

          
                  
          1. <script>
          2. var mshare = new mShare({
          3. title: 'Lorem ipsum dolor sit.',
          4. url: 'http://m.ly.com',
          5. desc: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quaerat inventore minima voluptates.',
          6. img: 'http://placehold.it/150x150'
          7. });
          8. $('button').click(function () {
          9. // 1 ==> 朋友圈 2 ==> 朋友 0 ==> 直接彈出原生
          10. mshare.init(+$(this).data('mshare'));
          11. });
          12. </script>

          下面是mshare.js的代碼分享,把這些代碼新建一個js文件放進去,然后在頁面中引進就ok了。

          
                  
          1. /**
          2. * 此插件主要作用是在UC和QQ兩個主流瀏覽器
          3. * 上面觸發微信分享到朋友圈或發送給朋友的功能
          4. */
          5. 'use strict';
          6. var UA = navigator.appVersion;
          7. /**
          8. * 是否是 UC 瀏覽器
          9. */
          10. var uc = UA.split('UCBrowser/').length > 1 ? 1 : 0;
          11. /**
          12. * 判斷 qq 瀏覽器
          13. * 然而qq瀏覽器分高低版本
          14. * 2 代表高版本
          15. * 1 代表低版本
          16. */
          17. var qq = UA.split('MQQBrowser/').length > 1 ? 2 : 0;
          18. /**
          19. * 是否是微信
          20. */
          21. var wx = /micromessenger/i.test(UA);
          22. /**
          23. * 瀏覽器版本
          24. */
          25. var qqVs = qq ? parseFloat(UA.split('MQQBrowser/')[1]) : 0;
          26. var ucVs = uc ? parseFloat(UA.split('UCBrowser/')[1]) : 0;
          27. /**
          28. * 獲取操作系統信息 iPhone(1) Android(2)
          29. */
          30. var os = (function () {
          31. var ua = navigator.userAgent;
          32. if (/iphone|ipod/i.test(ua)) {
          33. return 1;
          34. } else if (/android/i.test(ua)) {
          35. return 2;
          36. } else {
          37. return 0;
          38. }
          39. }());
          40. /**
          41. * qq瀏覽器下面 是否加載好了相應的api文件
          42. */
          43. var qqBridgeLoaded = false;
          44. // 進一步細化版本和平臺判斷
          45. if ((qq && qqVs < 5.4 && os == 1) || (qq && qqVs < 5.3 && os == 1)) {
          46. qq = 0;
          47. } else {
          48. if (qq && qqVs < 5.4 && os == 2) {
          49. qq = 1;
          50. } else {
          51. if (uc && ((ucVs < 10.2 && os == 1) || (ucVs < 9.7 && os == 2))) {
          52. uc = 0;
          53. }
          54. }
          55. }
          56. /**
          57. * qq瀏覽器下面 根據不同版本 加載對應的bridge
          58. * @method loadqqApi
          59. * @param {Function} cb 回調函數
          60. */
          61. function loadqqApi(cb) {
          62. // qq == 0
          63. if (!qq) {
          64. return cb && cb();
          65. }
          66. var script = document.createElement('script');
          67. script.src = (+qq === 1) ? '//3gimg.qq.com/html5/js/qb.js' : '//jsapi.qq.com/get?api=app.share';
          68. /**
          69. * 需要等加載過 qq 的 bridge 腳本之后
          70. * 再去初始化分享組件
          71. */
          72. script.onload = function () {
          73. cb && cb();
          74. };
          75. document.body.appendChild(script);
          76. }
          77. /**
          78. * UC瀏覽器分享
          79. * @method ucShare
          80. */
          81. function ucShare(config) {
          82. // ['title', 'content', 'url', 'platform', 'disablePlatform', 'source', 'htmlID']
          83. // 關于platform
          84. // ios: kWeixin || kWeixinFriend;
          85. // android: WechatFriends || WechatTimeline
          86. // uc 分享會直接使用截圖
          87. var platform = '';
          88. var shareInfo = null;
          89. // 指定了分享類型
          90. if (config.type) {
          91. if (os == 2) {
          92. platform = config.type == 1 ? 'WechatTimeline' : 'WechatFriends';
          93. } else if (os == 1) {
          94. platform = config.type == 1 ? 'kWeixinFriend' : 'kWeixin';
          95. }
          96. }
          97. shareInfo = [config.title, config.desc, config.url, platform, '', '', ''];
          98. // android
          99. if (window.ucweb) {
          100. ucweb.startRequest && ucweb.startRequest('shell.page_share', shareInfo);
          101. return;
          102. }
          103. if (window.ucbrowser) {
          104. ucbrowser.web_share && ucbrowser.web_share.apply(null, shareInfo);
          105. return;
          106. }
          107. }
          108. /**
          109. * qq 瀏覽器分享函數
          110. * @method qqShare
          111. */
          112. function qqShare(config) {
          113. var type = config.type;
          114. //微信好友 1, 微信朋友圈 8
          115. type = type ? ((type == 1) ? 8 : 1) : '';
          116. var share = function () {
          117. var shareInfo = {
          118. 'url': config.url,
          119. 'title': config.title,
          120. 'description': config.desc,
          121. 'img_url': config.img,
          122. 'img_title': config.title,
          123. 'to_app': type,
          124. 'cus_txt': ''
          125. };
          126. if (window.browser) {
          127. browser.app && browser.app.share(shareInfo);
          128. } else if (window.qb) {
          129. qb.share && qb.share(shareInfo);
          130. }
          131. };
          132. if (qqBridgeLoaded) {
          133. share();
          134. } else {
          135. loadqqApi(share);
          136. }
          137. }
          138. /**
          139. * 對外暴露的接口函數
          140. * @method mShare
          141. * @param {Object} config 配置對象
          142. */
          143. function mShare(config) {
          144. this.config = config;
          145. this.init = function (type) {
          146. if (typeof type != 'undefined') this.config.type = type;
          147. try {
          148. if (uc) {
          149. ucShare(this.config);
          150. } else if (qq && !wx) {
          151. qqShare(this.config);
          152. }
          153. } catch (e) {}
          154. }
          155. }
          156. // 預加載 qq bridge
          157. loadqqApi(function () {
          158. qqBridgeLoaded = true;
          159. });
          160. if (typeof module === 'object' && module.exports) {
          161. module.exports = mShare;
          162. } else {
          163. window.mShare = mShare;
          164. }

          好了,這樣就可以直接喚起微信進行分享啦

          藍藍設計www.syprn.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的UI界面設計、BS界面設計 、 cs界面設計  ipad界面設計 、 包裝設計  圖標定制 、 用戶體驗 、交互設計、 網站建設 、平面設計服務。

          日歷

          鏈接

          個人資料

          藍藍設計的小編 http://www.syprn.cn

          存檔

          亚洲va欧美va天堂v国产综合