6

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.

areim
  • 3,371
  • 2
  • 23
  • 29

1 Answers1

3

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

Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
  • 2
    This does not check if Whatsapp is installed; it just check the mobile phone OS. – Ulysses Alves Mar 02 '16 at 14:49
  • 5
    @UlyssesAlves it can't, and it shouldn't. in fact, if it would be possible to do that kind of check from the confines of the browser, that'd mean an enormous security / privacy breach. – Timothy Groote Apr 07 '16 at 08:01