如何在whatsapp共享消息

时间:2017-12-09 14:24:24

标签: javascript html

用于分享我的网站链接到whatsapp我在我的网站上使用了一个whatsapp共享按钮,这就是我从用户那里获取输入的原因, 例如。 我的网站名称是abcd.com,我在网站上创建了一个变量name, 我想发送 abcd.com/?name = userinput

等链接

我该怎么做

<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>

1 个答案:

答案 0 :(得分:0)

你可以这样做:

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>

完整的HTML代码:

<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>