areim areim - 4 months ago 23
CSS Question

WhatsApp share button on website. Easy way to detect if WhatsApp exists?

I'm adding WhatsApp share button on my website and I would like to hide this button when WhatsApp functionality does not exists (is not supported) on user device. Is there an easy way? Or any way?

I found http://whatsapp-sharing.com, but it has some disadvantages for me.
- no custom buttons/icons supported
- looks like it's detecting only Android and IOs (what about Windows Phone?)
- hard to maintain on bigger project

I'm searching for some JS/jQuery or maybe CSSonly (mediaqueries?) solution, but without success for now.
Any advice would be helpfull, thanks.

Answer

DEMO

Try this

$(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