简体   繁体   中英

How to share image and text on whatsapp using javascript

Hello till now i can share my content on whatsapp using javascript code but still not able to share image with text. Does anyone done it? Here is my javascript code:

$(document).ready(function() {
  $(document).on("click", '.mct_whatsapp_btn', function() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      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 use an Mobile Device to Share this Article");
    }
  });
});

If you are trying to do this from browser then as per whatsapp docs, you can only send text or link through this. read it here : https://www.whatsapp.com/faq/en/iphone/23559013

If you want to send image with this whatsapp protocol, you can first upload image to your server and then use 'encodeURIComponent' to send link with this:

'whatsapp://send?text='+encodeURIComponent(imageURL)

As already pointed by @Akis, for using it in hybrid app framework like cordova/ionic, its better if you search for plugin to achieve this.

https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin

Above link with cordova plugin might help you in that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM