mufazmi
mufazmi

Reputation: 1210

how to write user input in whatsapp sharing message

In my website I have a whatsapp sharing button, so i want to send my website link with a parameter,

For e.g.

abcd.com/?name=userinput

This is the code of whatsapp sharing button.

<script type="text/javascript">
var userinput=prompt("Please Enter Your Name");
</script>
<script><a href="whatsapp://send?text= Link abcd.html?name="><b>share on whatsapp</b></script>

Upvotes: 1

Views: 2879

Answers (1)

Eddie
Eddie

Reputation: 26854

You can do this:

JS:

<script>
    //Ask the user for name and store it on name variable
    var name = prompt("Please Enter Your Name");

    //This will be called when the link is clicked
    function sendWhatsapp(){
        var url     = "www.abcd.com/?name=" + name;
        var sMsg    = encodeURIComponent( "hi this is " + name + " and my link is " + url );
        var whatsapp_url = "whatsapp://send?text=" + sMsg;
        window.location.href = whatsapp_url;
    }
</script>

HTML:

<a onclick="sendWhatsapp()">share on whatsapp</a>

FULL HTML CODE:

<html>
<head>
<script>
    //Ask the user for name and store it on name variable
    var name = prompt("Please Enter Your Name");

    //This will be called when the link is clicked
    function sendWhatsapp(){
        var url     = "www.abcd.com/?name=" + name;
        var sMsg    = encodeURIComponent( "hi this is " + name + " and my link is " + url );
        var whatsapp_url = "whatsapp://send?text=" + sMsg;
        window.location.href = whatsapp_url;
    }
</script>
</head>
<body>
    <a onclick="sendWhatsapp()">share on whatsapp</a>
</body>
</html>

Upvotes: 1

Related Questions