Create line break in WhatsApp message

I was working on a WhatsApp exchange post using:

Whatsapp-button.js

$_u.="Product Name:".$_productName."\n";

$_u.="Sku:".$_productSku."\n";

<a href="whatsapp://send" data-text="<?php echo nl2br($_u); ?>" data-href="" class="wa_btn <?php echo $buttonsize; ?>" style="display:none">Share</a>

How to add line break:

I tried to \n, \r\n, PHP_EOLand %0D%0A, but it is simply displayed as text.

+7
source share
3 answers

I have one working solution:

HTML:

$_u.="Product Name:".$_productName."\n";
$_u.="Sku:".$_productSku."\n";

<a data-text="<?php echo $_u; ?>" data-link="" class="whatsapp">Share</a>

JS:

           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 in mobile device");
                    }

                });
+1
source

There is a solution in there is a solution that mainly uses:

whatsappMessage = window.encodeURIComponent(whatsappMessage)
0
source

use this %0a


link ='whatsapp://send?text=%0a‎Hello%0aWorld';

,

var encodedURL = encodeURIComponent(some_url);
link ='whatsapp://send?text=${encodedURL}%0a‎Hello%0aWorld';

<a href=link> Click here! </a>
0

Source: https://habr.com/ru/post/1569994/


All Articles