2016-03-31 11 views
3

Ich versuche, mit den folgenden Javascript und HTML den aktuellen Link in whatsapp zu teilenWhatsApp: Un der Lage, mit Javascript aktuellen Link zu teilen

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

Ich habe keine Ahnung, warum es nicht funktioniert, ich bin immer diese Ausgabe in einem Browser nach Drücken der Taste:

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

+0

haben Sie versucht, die URL zu entschlüsseln (zB% 20 statt Leerzeichen) 'encodeURIComponent()' – JoSSte

Antwort

1

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/

1

Ich weiß, das ist ein bisschen mehr wortreich ist als Sie wollen, aber es funktioniert, und Sie können auch benutzerdefinierte CSS hinzufügen.

$(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"); 
      } 

     }); 
    }); 
Verwandte Themen