29 Sep 2018

Click to send whatsapp with javascript

javascript tips

TL;DR You can use link below and place it on your button, or whatever. Its simply will open up whatsapp on user phone. But it isn't automatically send. https://api.whatsapp.com/send?phone=+YOURNUMBER&text=%20YOURMESSAGE. use encodeURIComponent('your text here')

What we were built here it is a dynamic send to WhatsApp link with JavaScript approach you can use the link above and give it a try

Okay I'll use JSBin with this, first open up then its just easy you will use split() and join() string property on JavaScript here my script.

// https://api.whatsapp.com/send?phone=+&text=%20

var yourNumber = "";
var yourMessage = "";

// %20 mean space in link
// If you already had an array then you just join them with '%20'
// easy right

function getLinkWhastapp(number, message) {
number = yourNumber;
message = yourMessage.split(" ").join("%20");

return console.log(
"https://api.whatsapp.com/send?phone=" + number + "&text=%20" + message
);
}

getLinkWhastapp();

Copy that on jsbin, give phone number and message do you want, make sure you use WhatsApp, It will open up WhatsApp on your phone with the message you wrote.

Further reading :


Important #

This post was made to learn JS split and join for production purpose you should use encodeURIComponent()

function getLinkWhastapp(number, message) {
var url =
"https://api.whatsapp.com/send?phone=" +
number +
"&text=" +
encodeURIComponent(message);

return url;
}