Whatsapp:无法与Javascript共享当前链接

时间:2016-03-31 13:04:47

标签: javascript html whatsapp

我正在尝试使用以下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

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:1)

我知道这比你想要的要多一些,但是它有效,你还可以添加自定义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");
            }

        });
    });