Home  >  Article  >  Web Front-end  >  WeChat browser built-in JavaScript object WeixinJSBridge usage example_javascript skills

WeChat browser built-in JavaScript object WeixinJSBridge usage example_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:57:471966browse

The WeChat public platform has begun to support front-end web pages. You may see that many web pages have buttons such as Share to Moments and Follow WeChat. Clicking on them will pop up a window for you to share and follow. How is this achieved? Today I will explain to you how to add share to Moments, follow WeChat ID and other buttons on the front-end web page of the WeChat public platform.

1. WeChat built-in browser

By remotely debugging WeChat’s own webpage on iPhone through Mac, we can find that WeChat’s embedded browser defines a private JavaScript object: WeixinJSBridge. By operating the relevant methods of this object, we can share it with WeChat Moments and judge a WeChat friend. The attention status of the signal and the realization of functions such as following the designated WeChat account.

2. js function: Share to Moments

Copy code The code is as follows:

function weixinShareTimeline(title,desc,link,imgUrl){
WeixinJSBridge.invoke(‘shareTimeline’,{
"img_url":imgUrl,
//"img_width":"640",
//"img_height":"640",
"link":link,
"desc": desc,
"title":title
});
}

3. js function: send to friends

Copy code The code is as follows:

function weixinSendAppMessage(title,desc,link,imgUrl){
WeixinJSBridge.invoke('sendAppMessage',{
//"appid":appId,
"img_url":imgUrl,
//"img_width":"640",
//"img_height":"640",
"link":link,
"desc":desc,
"title":title
});
}

4. js function: Share to Tencent Weibo

Copy code The code is as follows:

function weixinShareWeibo(title,link){
WeixinJSBridge.invoke(‘shareWeibo’,{
"content":title link,
"url":link
});
}

5. js function: follow the designated WeChat account

Copy code The code is as follows:


function weixinAddContact(name){
WeixinJSBridge.invoke(“addContact”, {webtype: “1″,username: name}, function(e) {
WeixinJSBridge.log(e.err_msg);
//e.err_msg:add_contact:added has been added
//e.err_msg:add_contact:cancel Cancel add
//e.err_msg:add_contact:ok Added successfully
if(e.err_msg == ‘add_contact:added’ || e.err_msg == ‘add_contact:ok’){
//Follow successfully, or have already followed
}
})
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn