【问题标题】:WhatsApp share button on website. Easy way to detect if WhatsApp exists?网站上的 WhatsApp 分享按钮。检测WhatsApp是否存在的简单方法?
【发布时间】:2015-09-13 04:33:52
【问题描述】:

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

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

我正在寻找一些 JS/jQuery 或者 CSSonly(媒体查询?)解决方案,但目前没有成功。 任何建议都会有所帮助,谢谢。

【问题讨论】:

    标签: javascript jquery css whatsapp


    【解决方案1】:

    DEMO

    试试这个

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

    SOURCE

    【讨论】:

    • 这不会检查是否安装了 Whatsapp;它只是检查手机操作系统。
    • @UlyssesAlves 它不能,也不应该。事实上,如果可以在浏览器的范围内进行这种检查,那将意味着巨大的安全/隐私泄露。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    相关资源
    最近更新 更多