jQuery code to share content on whatsapp

In this short post, find jQuery code to share any content on whatsapp. The code first checks for mobile device and if it's a mobile device then it just creates whatsapp URL with user defined text and link to share and then just redirects it. That's it.

jQuery code to share content on whatsapp

$(document).ready(function () {
   $(document).on("click", '.whatsapp', function () {
      if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
        var sText = "Text to share"; 
        var sUrl = "Link to share";
        var sMsg = encodeURIComponent(sText) + " - " + encodeURIComponent(sUrl);
        var whatsapp_url = "whatsapp://send?text=" + sMsg;
        window.location.href = whatsapp_url;
     } 
     else {
        alert("Whatsapp client not available.");
     }
  });
});
You may also like:
Feel free to contact me for any help related to jQuery, I will gladly help you.
SHARE

Nothing shocks me, I'm a Software Engineer. And I am not young enough to know everything. I live in World Wide Web and from there take care of this website. This website communicates about my work, learning and experience. I believe life is short, and it is for loving, sharing, learning and connecting. So lets connect..