4

i am trying to share the current link in whatsapp by using the following javascript and HTML

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

I have have no idea why it is not working i am getting this output in a browser after pressing the button:

whatsapp://send?text=Check this out: http://bggressive.nl/test/index.html

Hamza Zafeer
  • 2,360
  • 13
  • 30
  • 42
Bas Mienis
  • 99
  • 1
  • 7

3 Answers3

2

try this:

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

warch
  • 2,387
  • 2
  • 26
  • 43
1

I know this is a bit more wordy than you want, but it works, and you can also add custom 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");
            }

        });
    });
TechnicalTophat
  • 1,655
  • 1
  • 15
  • 37
0

Below is an example function to forward the current url to the Whatsapp API:

function getURL() {
    open(encodeURI("https://api.whatsapp.com/send?text="+window.location.href));
}
 
<button type="button" onclick="getURL();">Whatsapp</button>
Justus Romijn
  • 15,699
  • 5
  • 51
  • 63
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 19 '21 at 12:52