【问题标题】:Whatsapp: Un able to share current link with JavascriptWhatsapp:无法使用 Javascript 共享当前链接
【发布时间】:2016-07-19 23:07:19
【问题描述】:

我正在尝试使用以下 javascript 和 HTML 分享 whatsapp 中的当前链接

<script language="javascript">
    function waCurrentPage(){
        return "whatsapp://send?text=Check this out: "+'http://' +
        window.location.hostname + window.location.pathname;
   }
</script>


<a  class="btn btn-social-icon btn-whatsapp" href="javascript:waCurrentPage()" 
    data-action="share/whatsapp/share"><i class="fa fa-whatsapp"></i>
</a>

我不知道为什么它不起作用我在按下按钮后在浏览器中得到这个输出:

whatsapp://send?text=看看这个:http://bggressive.nl/test/index.html

【问题讨论】:

  • 您是否尝试过转义 URL(即 %20 而不是空格)encodeURIComponent()

标签: javascript html whatsapp


【解决方案1】:

以下是将当前 url 转发到 Whatsapp API 的示例函数:

function getURL() {
    open(encodeURI("https://api.whatsapp.com/send?text="+window.location.href));
}
 
<button type="button" onclick="getURL();">Whatsapp</button>

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

试试这个:

<a class="btn btn-social-icon btn-whatsapp" href="javascript:window.location=waCurrentPage();">Link</a>

JS:

waCurrentPage = function() {
   return encodeURI("whatsapp://send?text=Check this out: " + 'http://' + window.location.hostname + window.location.pathname);
}

https://jsfiddle.net/7ny07Lfw/19/

【讨论】:

    【解决方案3】:

    我知道这比你想要的有点罗嗦,但它有效,你也可以添加自定义 css。

    $(document).ready(function() {
        var isMobile = {
            Android: function() {
                return navigator.userAgent.match(/Android/i);
            },
    
            BlackBerry: function() {
                return navigator.userAgent.match(/BlackBerry/i);
            },
            iOS: function() {
                return navigator.userAgent.match(/iPhone|iPad|iPod/i);
            },
            Opera: function() {
                return navigator.userAgent.match(/Opera Mini/i);
            },
            Windows: function() {
                return navigator.userAgent.match(/IEMobile/i);
            },
            any: function() {
                return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
            }
        };
        $(document).on("click", '.whatsapp', function() {
                if( isMobile.any() ) {
    
                    var text = $(this).attr("data-text");
                    var url = $(this).attr("data-link");
                    var message = encodeURIComponent(text) + " - " + encodeURIComponent(url);
                    var whatsapp_url = "whatsapp://send?text=" + message;
                    window.location.href = whatsapp_url;
                } else {
                    alert("Please share this article in mobile device");
                }
    
            });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      相关资源
      最近更新 更多