This article mainly introduces js to realize the display of friend links shared by WeChat. It is helpful for learning ASP.NETprogramming technology. Interested readers Friends, you can refer to
Usually when I make a page and want to share it with friends through WeChat, the title and description displayed are not what I want. I checked some information and found out that it is done through js. The control
display effect is as follows:
##title, description, and shared Pictures are all controlled by js. js code is as follows
<script> var dataForWeixin = { appId: "", MsgImg: "Christmas/201012189457639.gif",//顯示圖片 TLImg: "Christmas/201012189457639.gif",//顯示圖片 url: "Christmas/6.html?stra=!u738B!u4F1F",//跳轉(zhuǎn)地址 title: "將我的思念和祝福送給您,頤養(yǎng)源祝大家圣誕快樂",//標題內(nèi)容 desc: "將我的思念和祝福送給您,頤養(yǎng)源祝大家圣誕快樂",//描述內(nèi)容 fakeid: "", callback: function () { } }; (function () { var onBridgeReady = function () { WeixinJSBridge.on('menu:share:appmessage', function (argv) { WeixinJSBridge.invoke('sendAppMessage', { "appid": dataForWeixin.appId, "img_url": dataForWeixin.MsgImg, "img_width": "120", "img_height": "120", "link": dataForWeixin.url, "desc": dataForWeixin.desc, "title": dataForWeixin.title }, function (res) { (dataForWeixin.callback)(); }); }); WeixinJSBridge.on('menu:share:timeline', function (argv) { (dataForWeixin.callback)(); WeixinJSBridge.invoke('shareTimeline', { "img_url": dataForWeixin.TLImg, "img_width": "120", "img_height": "120", "link": dataForWeixin.url, "desc": dataForWeixin.desc, "title": dataForWeixin.title }, function (res) { }); }); WeixinJSBridge.on('menu:share:weibo', function (argv) { WeixinJSBridge.invoke('shareWeibo', { "content": dataForWeixin.title, "url": dataForWeixin.url }, function (res) { (dataForWeixin.callback)(); }); }); WeixinJSBridge.on('menu:share:facebook', function (argv) { (dataForWeixin.callback)(); WeixinJSBridge.invoke('shareFB', { "img_url": dataForWeixin.TLImg, "img_width": "120", "img_height": "120", "link": dataForWeixin.url, "desc": dataForWeixin.desc, "title": dataForWeixin.title }, function (res) { }); }); }; if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } })(); </script>Another WeChat share js code:
/**! * 微信內(nèi)置瀏覽器的Javascript API,功能包括: * * 1、分享到微信朋友圈 * 2、分享給微信好友 * 3、分享到騰訊微博 * 4、新的分享接口,包含朋友圈、好友、微博的分享(for iOS) * 5、隱藏/顯示右上角的菜單入口 * 6、隱藏/顯示底部瀏覽器工具欄 * 7、獲取當(dāng)前的網(wǎng)絡(luò)狀態(tài) * 8、調(diào)起微信客戶端的圖片播放組件 * 9、關(guān)閉公眾平臺Web頁面 */ var WeixinApi = (function () { "use strict"; /** * 分享到微信朋友圈 * @param {Object} data 待分享的信息 * @p-config {String} appId 公眾平臺的appId(服務(wù)號可用) * @p-config {String} imgUrl 圖片地址 * @p-config {String} link 鏈接地址 * @p-config {String} desc 描述 * @p-config {String} title 分享的標題 * * @param {Object} callbacks 相關(guān)回調(diào)方法 * @p-config {Boolean} async ready方法是否需要異步執(zhí)行,默認false * @p-config {Function} ready(argv) 就緒狀態(tài) * @p-config {Function} dataLoaded(data) 數(shù)據(jù)加載完成后調(diào)用,async為true時有用,也可以為空 * @p-config {Function} cancel(resp) 取消 * @p-config {Function} fail(resp) 失敗 * @p-config {Function} confirm(resp) 成功 * @p-config {Function} all(resp) 無論成功失敗都會執(zhí)行的回調(diào) */ function weixinShareTimeline(data, callbacks) { callbacks = callbacks || {}; var shareTimeline = function (theData) { WeixinJSBridge.invoke('shareTimeline', { "appid":theData.appId ? theData.appId : '', "img_url":theData.imgUrl, "link":theData.link, "desc":theData.title, "title":theData.desc, // 注意這里要分享出去的內(nèi)容是desc "img_width":"640", "img_height":"640" }, function (resp) { switch (resp.err_msg) { // share_timeline:cancel 用戶取消 case 'share_timeline:cancel': callbacks.cancel && callbacks.cancel(resp); break; // share_timeline:confirm 發(fā)送成功 case 'share_timeline:confirm': case 'share_timeline:ok': callbacks.confirm && callbacks.confirm(resp); break; // share_timeline:fail 發(fā)送失敗 case 'share_timeline:fail': default: callbacks.fail && callbacks.fail(resp); break; } // 無論成功失敗都會執(zhí)行的回調(diào) callbacks.all && callbacks.all(resp); }); }; WeixinJSBridge.on('menu:share:timeline', function (argv) { if (callbacks.async && callbacks.ready) { window["_wx_loadedCb_"] = callbacks.dataLoaded || new Function(); if(window["_wx_loadedCb_"].toString().indexOf("_wx_loadedCb_") > 0) { window["_wx_loadedCb_"] = new Function(); } callbacks.dataLoaded = function (newData) { window["_wx_loadedCb_"](newData); shareTimeline(newData); }; // 然后就緒 callbacks.ready && callbacks.ready(argv); } else { // 就緒狀態(tài) callbacks.ready && callbacks.ready(argv); shareTimeline(data); } }); } /** * 發(fā)送給微信上的好友 * @param {Object} data 待分享的信息 * @p-config {String} appId 公眾平臺的appId(服務(wù)號可用) * @p-config {String} imgUrl 圖片地址 * @p-config {String} link 鏈接地址 * @p-config {String} desc 描述 * @p-config {String} title 分享的標題 * * @param {Object} callbacks 相關(guān)回調(diào)方法 * @p-config {Boolean} async ready方法是否需要異步執(zhí)行,默認false * @p-config {Function} ready(argv) 就緒狀態(tài) * @p-config {Function} dataLoaded(data) 數(shù)據(jù)加載完成后調(diào)用,async為true時有用,也可以為空 * @p-config {Function} cancel(resp) 取消 * @p-config {Function} fail(resp) 失敗 * @p-config {Function} confirm(resp) 成功 * @p-config {Function} all(resp) 無論成功失敗都會執(zhí)行的回調(diào) */ function weixinSendAppMessage(data, callbacks) { callbacks = callbacks || {}; var sendAppMessage = function (theData) { WeixinJSBridge.invoke('sendAppMessage', { "appid":theData.appId ? theData.appId : '', "img_url":theData.imgUrl, "link":theData.link, "desc":theData.desc, "title":theData.title, "img_width":"640", "img_height":"640" }, function (resp) { switch (resp.err_msg) { // send_app_msg:cancel 用戶取消 case 'send_app_msg:cancel': callbacks.cancel && callbacks.cancel(resp); break; // send_app_msg:confirm 發(fā)送成功 case 'send_app_msg:confirm': case 'send_app_msg:ok': callbacks.confirm && callbacks.confirm(resp); break; // send_app_msg:fail 發(fā)送失敗 case 'send_app_msg:fail': default: callbacks.fail && callbacks.fail(resp); break; } // 無論成功失敗都會執(zhí)行的回調(diào) callbacks.all && callbacks.all(resp); }); }; WeixinJSBridge.on('menu:share:appmessage', function (argv) { if (callbacks.async && callbacks.ready) { window["_wx_loadedCb_"] = callbacks.dataLoaded || new Function(); if(window["_wx_loadedCb_"].toString().indexOf("_wx_loadedCb_") > 0) { window["_wx_loadedCb_"] = new Function(); } callbacks.dataLoaded = function (newData) { window["_wx_loadedCb_"](newData); sendAppMessage(newData); }; // 然后就緒 callbacks.ready && callbacks.ready(argv); } else { // 就緒狀態(tài) callbacks.ready && callbacks.ready(argv); sendAppMessage(data); } }); } /** * 分享到騰訊微博 * @param {Object} data 待分享的信息 * @p-config {String} link 鏈接地址 * @p-config {String} desc 描述 * * @param {Object} callbacks 相關(guān)回調(diào)方法 * @p-config {Boolean} async ready方法是否需要異步執(zhí)行,默認false * @p-config {Function} ready(argv) 就緒狀態(tài) * @p-config {Function} dataLoaded(data) 數(shù)據(jù)加載完成后調(diào)用,async為true時有用,也可以為空 * @p-config {Function} cancel(resp) 取消 * @p-config {Function} fail(resp) 失敗 * @p-config {Function} confirm(resp) 成功 * @p-config {Function} all(resp) 無論成功失敗都會執(zhí)行的回調(diào) */ function weixinShareWeibo(data, callbacks) { callbacks = callbacks || {}; var shareWeibo = function (theData) { WeixinJSBridge.invoke('shareWeibo', { "content":theData.desc, "url":theData.link }, function (resp) { switch (resp.err_msg) { // share_weibo:cancel 用戶取消 case 'share_weibo:cancel': callbacks.cancel && callbacks.cancel(resp); break; // share_weibo:confirm 發(fā)送成功 case 'share_weibo:confirm': case 'share_weibo:ok': callbacks.confirm && callbacks.confirm(resp); break; // share_weibo:fail 發(fā)送失敗 case 'share_weibo:fail': default: callbacks.fail && callbacks.fail(resp); break; } // 無論成功失敗都會執(zhí)行的回調(diào) callbacks.all && callbacks.all(resp); }); }; WeixinJSBridge.on('menu:share:weibo', function (argv) { if (callbacks.async && callbacks.ready) { window["_wx_loadedCb_"] = callbacks.dataLoaded || new Function(); if(window["_wx_loadedCb_"].toString().indexOf("_wx_loadedCb_") > 0) { window["_wx_loadedCb_"] = new Function(); } callbacks.dataLoaded = function (newData) { window["_wx_loadedCb_"](newData); shareWeibo(newData); }; // 然后就緒 callbacks.ready && callbacks.ready(argv); } else { // 就緒狀態(tài) callbacks.ready && callbacks.ready(argv); shareWeibo(data); } }); } /** * 新的分享接口 * @param {Object} data 待分享的信息 * @p-config {String} appId 公眾平臺的appId(服務(wù)號可用) * @p-config {String} imgUrl 圖片地址 * @p-config {String} link 鏈接地址 * @p-config {String} desc 描述 * @p-config {String} title 分享的標題 * * @param {Object} callbacks 相關(guān)回調(diào)方法 * @p-config {Boolean} async ready方法是否需要異步執(zhí)行,默認false * @p-config {Function} ready(argv,shareTo) 就緒狀態(tài) * @p-config {Function} dataLoaded(data) 數(shù)據(jù)加載完成后調(diào)用,async為true時有用,也可以為空 * @p-config {Function} cancel(resp,shareTo) 取消 * @p-config {Function} fail(resp,shareTo) 失敗 * @p-config {Function} confirm(resp,shareTo) 成功 * @p-config {Function} all(resp,shareTo) 無論成功失敗都會執(zhí)行的回調(diào) */ function weixinGeneralShare(data, callbacks) { callbacks = callbacks || {}; var generalShare = function (general,theData) { // 如果是分享到朋友圈,則需要把title和desc交換一下 if(general.shareTo == 'timeline') { var title = theData.title; theData.title = theData.desc || title; theData.desc = title; } // 分享出去 general.generalShare({ "appid":theData.appId ? theData.appId : '', "img_url":theData.imgUrl, "link":theData.link, "desc":theData.desc, "title":theData.title, "img_width":"640", "img_height":"640" }, function (resp) { switch (resp.err_msg) { // general_share:cancel 用戶取消 case 'general_share:cancel': callbacks.cancel && callbacks.cancel(resp ,general.shareTo); break; // general_share:confirm 發(fā)送成功 case 'general_share:confirm': case 'general_share:ok': callbacks.confirm && callbacks.confirm(resp ,general.shareTo); break; // general_share:fail 發(fā)送失敗 case 'general_share:fail': default: callbacks.fail && callbacks.fail(resp ,general.shareTo); break; } // 無論成功失敗都會執(zhí)行的回調(diào) callbacks.all && callbacks.all(resp ,general.shareTo); }); }; WeixinJSBridge.on('menu:general:share', function (general) { if (callbacks.async && callbacks.ready) { window["_wx_loadedCb_"] = callbacks.dataLoaded || new Function(); if(window["_wx_loadedCb_"].toString().indexOf("_wx_loadedCb_") > 0) { window["_wx_loadedCb_"] = new Function(); } callbacks.dataLoaded = function (newData) { window["_wx_loadedCb_"](newData); generalShare(general,newData); }; // 然后就緒 callbacks.ready && callbacks.ready(general,general.shareTo); } else { // 就緒狀態(tài) callbacks.ready && callbacks.ready(general,general.shareTo); generalShare(general,data); } }); } /** * 加關(guān)注(此功能只是暫時先加上,不過因為權(quán)限限制問題,不能用,如果你的站點是部署在*.qq.com下,也許可行) * @param {String} appWeixinId 微信公眾號ID * @param {Object} callbacks 回調(diào)方法 * @p-config {Function} fail(resp) 失敗 * @p-config {Function} confirm(resp) 成功 */ function addContact(appWeixinId,callbacks){ callbacks = callbacks || {}; WeixinJSBridge.invoke("addContact", { webtype: "1", username: appWeixinId }, function (resp) { var success = !resp.err_msg || "add_contact:ok" == resp.err_msg || "add_contact:added" == resp.err_msg; if(success) { callbacks.success && callbacks.success(resp); }else{ callbacks.fail && callbacks.fail(resp); } }) } /** * 調(diào)起微信Native的圖片播放組件。 * 這里必須對參數(shù)進行強檢測,如果參數(shù)不合法,直接會導(dǎo)致微信客戶端crash * * @param {String} curSrc 當(dāng)前播放的圖片地址 * @param {Array} srcList 圖片地址列表 */ function imagePreview(curSrc,srcList) { if(!curSrc || !srcList || srcList.length == 0) { return; } WeixinJSBridge.invoke('imagePreview', { 'current' : curSrc, 'urls' : srcList }); } /** * 顯示網(wǎng)頁右上角的按鈕 */ function showOptionMenu() { WeixinJSBridge.call('showOptionMenu'); } /** * 隱藏網(wǎng)頁右上角的按鈕 */ function hideOptionMenu() { WeixinJSBridge.call('hideOptionMenu'); } /** * 顯示底部工具欄 */ function showToolbar() { WeixinJSBridge.call('showToolbar'); } /** * 隱藏底部工具欄 */ function hideToolbar() { WeixinJSBridge.call('hideToolbar'); } /** * 返回如下幾種類型: * * network_type:wifi wifi網(wǎng)絡(luò) * network_type:edge 非wifi,包含3G/2G * network_type:fail 網(wǎng)絡(luò)斷開連接 * network_type:wwan 2g或者3g * * 使用方法: * WeixinApi.getNetworkType(function(networkType){ * * }); * * @param callback */ function getNetworkType(callback) { if (callback && typeof callback == 'function') { WeixinJSBridge.invoke('getNetworkType', {}, function (e) { // 在這里拿到e.err_msg,這里面就包含了所有的網(wǎng)絡(luò)類型 callback(e.err_msg); }); } } /** * 關(guān)閉當(dāng)前微信公眾平臺頁面 */ function closeWindow() { WeixinJSBridge.call("closeWindow"); } /** * 當(dāng)頁面加載完畢后執(zhí)行,使用方法: * WeixinApi.ready(function(Api){ * // 從這里只用Api即是WeixinApi * }); * @param readyCallback */ function wxJsBridgeReady(readyCallback) { if (readyCallback && typeof readyCallback == 'function') { var Api = this; var wxReadyFunc = function () { readyCallback(Api); }; if (typeof window.WeixinJSBridge == "undefined"){ if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', wxReadyFunc, false); } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', wxReadyFunc); document.attachEvent('onWeixinJSBridgeReady', wxReadyFunc); } }else{ wxReadyFunc(); } } } return { version :"2.0", ready :wxJsBridgeReady, shareToTimeline :weixinShareTimeline, shareToWeibo :weixinShareWeibo, shareToFriend :weixinSendAppMessage, generalShare :weixinGeneralShare, addContact :addContact, showOptionMenu :showOptionMenu, hideOptionMenu :hideOptionMenu, showToolbar :showToolbar, hideToolbar :hideToolbar, getNetworkType :getNetworkType, imagePreview :imagePreview, closeWindow :closeWindow }; })();The above is Sharing js code on WeChat, I hope it will be helpful to everyone's learning.
The above is the detailed content of Using js to implement WeChat sharing example code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ??and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
