javascript - 如何在whatsapp共享消息中写入用户输入

标签 javascript html

在我的网站中,我有一个 Whatsapp 共享按钮,因此我想发送带有参数的网站链接,

例如

abcd.com/?name=userinput

这是whatsapp分享按钮的代码。

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

最佳答案

你可以这样做:

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>

关于javascript - 如何在whatsapp共享消息中写入用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47729544/

相关文章:

javascript - 根据url新建背景

javascript - Fotorama 在所有浏览器中都在顶部对齐箭头?在 IE :( 中不工作

javascript - AngularJS:让元素跟随光标

html - 如何使 CSS HTML 选项卡默认显示

javascript - 使用虚拟键盘时 useState 的初始值未更新

javascript - 在更改数据源时显示子行

html - 当我将鼠标悬停在这些图像上时,如何让单词出现?

html - 我无法使用 CSS 正确设置 HTML 元素的高度

javascript - Twitter Bootstrap 3 模式对话框不显示

javascript - HTML5 Canvas API - 用斜体格式化单个单词