Share via WhatsApp only if installed

I’m trying to make sharing of WhatsApp resources (for a mobile site) available to visitors of the application.

What would be the best way to verify that a visitor can use this feature so that I can enable / disable it accordingly?

The function will be just a link using a custom url scheme:

<a href="whatsapp://send?text=Hello%20World!">Hello, world!</a>

+5
source share
2 answers

You can solve this problem by checking if the link opens or not.

Here is my code

 <a id="share_whatsapp" onclick="open_whatsapp()">Share with Whatsapp</a> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js?ver=4.1' type='text/javascript'></script> <script> function open_whatsapp(){ $.ajax({ type: 'HEAD', url: 'whatsapp://send?text=text=Hello%20World!', success: function() { window.location='whatsapp://send?text=text=Hello%20World!'; }, error: function() { alert("Whatspp not installed"); } }); } </script> 
+4
source

This is not a good solution because it depends on the OS.

 if(isMobile()){ function onWhatsAppClick(e){ e.preventDefault(); window.location='whatsapp://send?text=text=Hello%20World!'; } } 

I explain:

  • You should check and show WhatsApp only on mobile devices.
  • Prevent default and open link using window.location

Plunker: https://plnkr.co/edit/U4CtbxeA81d25lc7dlGe?p=preview

0
source

Source: https://habr.com/ru/post/1208449/


All Articles