网络版Whatsapp共享按钮



我有这个脚本,但它只是从移动版本共享的。

$(document).ready(function() {
$(document).on("click", '.mc_whatsapp_btn', function() {
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
var text = $(this).attr("data-text");
var url = $(this).attr("data-link");
var message = encodeURIComponent(text) + " - " + encodeURIComponent(url);
var whatsapp_url = ".whatsapp://send?text=" + message;
window.location.href =  whatsapp_url;
} else {
alert("Please use an Mobile Device to Share this Status");
}
});
});

有人能修改这个吗?

此行

if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {

检查mobile,如果为true,则执行共享逻辑。在else分支(意味着它不是移动设备(中,您会收到一条错误消息。如果你想在两个版本中都发生同样的情况,只需省略if:

$(document).ready(function() {
$(document).on("click", '.mc_whatsapp_btn', function() {
//if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
var text = $(this).attr("data-text");
var url = $(this).attr("data-link");
var message = encodeURIComponent(text) + " - " + encodeURIComponent(url);
var whatsapp_url = ".whatsapp://send?text=" + message;
window.location.href =  whatsapp_url;
//} else {
//alert("Please use an Mobile Device to Share this Status");
}
});
});

根据WhatsApp文档:https://faq.whatsapp.com/general/chats/how-to-use-click-to-chat/?lang=en

若要创建仅包含预填充邮件的链接,请使用https://wa.me/?text=urlencodedtext

示例:https://wa.me/?text=I'm%20查询%20关于%20公寓%20挂牌`

结果应该是:

$(document).ready(function() {
$(document).on("click", '.mc_whatsapp_btn', function() {
// this 3 rows will be used for both - desktop and mobile      
var text = $(this).attr("data-text");
var url = $(this).attr("data-link");
var message = encodeURIComponent(text) + " - " + encodeURIComponent(url);
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
var whatsapp_url = ".whatsapp://send?text=" + message;
} else {
var whatsapp_url = "https://wa.me/?text=" + message;
}
// again for both
window.location.href =  whatsapp_url;
});

});

最新更新