Share via WhatsApp only if it is installed

为君一笑 提交于 2020-01-13 08:55:12

问题


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>


回答1:


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("Whatspp 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>



回答2:


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



来源:https://stackoverflow.com/questions/27321472/share-via-whatsapp-only-if-it-is-installed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!