My readers keep on asking me that How to share information directly from the web page into WhatsApp? So I thought to write a simple tutorial which will help to share articles from the webpage to Whatsapp using jQuery.
Report says that by adding a Whatsapp sharing button on websites that will generate more shares than popular social sharing websites.
Before that I just want to explain you few things
Contents
Why Whatsapp?
WhatsApp is one of the most popular messaging apps in the world – Facebook bought whatapp for $19 billion.
One third of world’s population is using Whatsapp on their smartphones. So adding whatsapp share button in your website will be helpful to share content with anyone anytime anywhere!
.w3_whatsapp_btn {
background-image: url('icon.png');
border: 1px solid rgba(0, 0, 0, 0.1);
display: inline-block !important;
position: relative;
font-family: Arial,sans-serif;
letter-spacing: .4px;
cursor: pointer;
font-weight: 400;
text-transform: none;
color: #fff;
border-radius: 2px;
background-color: #5cbe4a;
background-repeat: no-repeat;
line-height: 1.2;
text-decoration: none;
text-align: left;
}
.w3_whatsapp_btn_small {
font-size: 12px;
background-size: 16px;
background-position: 5px 2px;
padding: 3px 6px 3px 25px;
}
.w3_whatsapp_btn_medium {
font-size: 16px;
background-size: 20px;
background-position: 4px 2px;
padding: 4px 6px 4px 30px;
}
.w3_whatsapp_btn_large {
font-size: 16px;
background-size: 20px;
background-position: 5px 5px;
padding: 8px 6px 8px 30px;
color: #fff;
}
a.whatsapp { color: #fff;}
I have designed three sizes of button with small, medium & large. Please use it based on your design template.
HTML Code
Please pass the title and url of the post in data-text and data-link attributes.
<a data-text="Your message goes here.." data-link="http://w3lessons.info" class="whatsapp w3_whatsapp_btn w3_whatsapp_btn_large">Share</a>
I have used Google’s jQuery CDN. Please place this code in bottom of the page
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
Below code is used to share the content on Whatsapp. By using the Class Name .whatsapp of the element, we are taking the title and link of the post using HTML5 Data Attributes data-text, data-link
The encodeURIComponent() function encodes a URI component. This function encodes special characters. In addition, it encodes the following characters: , / ? : @ & = + $ #
The IF statement is used to identify the mobile device because In desktop, whatsapp share will not work.
$(document).ready(function() {
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
$(document).on("click", '.whatsapp', function() {
if( isMobile.any() ) {
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 share this article in mobile device");
}
});
});
Please see the live demo in mobile device to share the articles on Whatsapp.
Live DemoI hope you like this article very much & Please dont forget the share and subscribe my blog to get the latest updates delivered in your email.
Thanks 🙂
Great job i like it.
Great job KARTHIKEYAN, It’s working awesome, but I want to share an image without URL, Could you please help me with the same.
hello,its not working in iPhone 4 and iPhone 5
Hi Sayyed,
Its working fine here.. could you please share the screenshot
Thanks
can this be implemented for whatsapp web using desktop site?
This is not working on iPhone. 🙁
that nice. how to get data-link= current link?
Can we make it for Whatsapp for web also ??
Hi,
It’s not working first time share. If I kept alert inside on click function and then click share It’s working. Any guess what could be the issue??
any code to share image also ???
I wanted to Share Content to all my Whatsapp contacts using jQuery .
is it possible ?
hi,
i am new for this can you tell me how can we test the same on pc/mobile phone????
Is it possible to share image with link?
Dear Karthikeyan,Really it is awesome job.
Your code is working fine for most of the browsers.
But when I tested it on windows phone’s default browser,it redirects to whatsapp but contents are empty.
May be browser is not compatible with the code.
But really thank you for this code
Thank you very much!! 🙂
I have tested it from windows phone.. its working fine from my end…
Please share the screenshot to test it out..
Thanks
Awesome Job Karthikeyan 🙂
Just now I have implemented in my project.. Works like a Charm…
Thanks
Awesome Tutorial.. Tested it from my iPhone.. Sharing on whatsapp rocks 🙂
Great Job Karthikeyan..
How to Share Content on viber using jQuery?
Karthikeyan, this is great. I just tested this on my Samsung Galaxy S4 IS. Thanks.