loading...

Click to send on WhatsApp with JavaScript

mandaputtra profile image Manda Putra Updated on ・1 min read

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=%20*YOURMESSAGE*

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=+{{ *YOURNUMBER* }}&text=%20{{ *YOUR MESSAGE* }}

var yourNumber = "{{ your number in string}}"
var yourMessage = "{{ your message in string }}"

// %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
}

Posted on by:

mandaputtra profile

Manda Putra

@mandaputtra

A former profesional billiard player, now playing with code.

Discussion

markdown guide
 

maybe use encodeURIComponent() to encode this.message to URI

 

Yeah thats nice, easy, error prone

 

Is there any way to send image with text?

 

No man its only accept url