繁体   English   中英

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

[英]Whatsapp: Un able to share current link with Javascript

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

试试这个:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM