问题
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:
- You should check and show WhatsApp on mobile only.
- 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