8

I'm trying to make a WhatsApp share option (for a mobile website) available to visitors that have the app installed.

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

The feature would just be a link using the custom URL scheme:

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

Jamie Kudla
  • 872
  • 3
  • 17
  • 37

2 Answers2

10

You can solve this by checking whether the link will open or not.

Here is my code

function open_whatsapp(){
    $.ajax({
      type: 'HEAD',
      url: 'whatsapp://send?text=Hello%20World!',
      success: function() {
        window.location='whatsapp://send?text=Hello%20World!';   
      },
      error: function() {
        alert("Whatsapp not installed");
      }
    });
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="share_whatsapp" onclick="open_whatsapp()">Share with Whatsapp</button>
Michiel Pater
  • 22,377
  • 5
  • 43
  • 57
Sree KS
  • 1,311
  • 1
  • 13
  • 26
  • 5
    I get the following error each time I execute open_whatsapp(): "Failed to load whatsapp://send?phone=+XXX: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https." – RaphArbuz Apr 23 '18 at 15:15
0

It is not good solution, because it depends on OS.

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

Explain:

  1. You should check and show WhatsApp on mobile only.
  2. Prevent default, and open the link with window.location

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

vanduc1102
  • 5,769
  • 1
  • 46
  • 43