WhatsApp分享网站上的按钮.检测WhatsApp是否存在的简便方法?

are*_*eim 6 javascript css jquery whatsapp

我正在我的网站上添加WhatsApp共享按钮,我想在用户设备上不存在(不支持)WhatsApp功能时隐藏此按钮.有一个简单的方法吗?或者任何方式?

我找到了http://whatsapp-sharing.com,但它对我来说有一些不利之处. - 没有支持自定义按钮/图标 - 看起来它只检测Android和IO(Windows Phone呢?) - 难以在更大的项目上维护

我正在寻找一些JS/jQuery或者CSSonly(mediaqueries?)解决方案,但现在还没有成功.任何建议都会有所帮助,谢谢.

Gur*_*Rao 3

演示版

尝试这个

$(document).ready(function() {

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};
if( isMobile.any() ) {
    //hide the share button
}
 $(document).on("click", '.whatsapp', function() {
        if( isMobile.any() ) {
            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 share this article in mobile device");
        }

    });
});
Run Code Online (Sandbox Code Playgroud)

来源

  • @UlyssesAlves 它不能,也不应该。事实上,如果可以在浏览器的范围内进行这种检查,那就意味着巨大的安全/隐私漏洞。 (5认同)
  • 这不会检查 Whatsapp 是否已安装;它只是检查手机操作系统。 (2认同)