网页版Whatsapp分享按钮

问题描述 投票:0回答:3

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

$(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");
        }
    });
});

任何人都可以修改这个吗?

javascript button share whatsapp
3个回答
0
投票

这条线

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

检查移动设备,如果为真,则执行共享逻辑。在 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");
        }
    });
});

0
投票

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

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

示例:https://wa.me/?text=我%20inquiring%20about%20the%20apartment%20listing`

结果应该是这样的:

$(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;
});

});


0
投票

遵循上述文档;这对我有用,适用于桌面和移动设备

<a href="https://wa.me/?text=Hey! Check this awesome page! https://stackoverflow.com/questions/65457914/whatsapp-share-button-for-web-version" target="_blank" rel="noopener noreferrer">
  Share by Whatsapp!
</a>

© www.soinside.com 2019 - 2024. All rights reserved.