7

I'm currently developing a web app and there is this part of the code where I have to pre-populate the message in the sms box. So my code looks like this:

    <a href="sms:?body=This is the message with & symbol">SMS</a>

In the pre-populated message, everything from the & symbol onwards does not appear in the message box on the phone. I know I have to encode it but I do not know what the encoding code is. Any solution to this? Thanks.

rene
  • 41,474
  • 78
  • 114
  • 152
Cirrus
  • 71
  • 1
  • 2
  • Even in 2017 this is still a problem. I think this question just hasn't garnered much attention because it's decently obscure; I get the feeling not many people generating links with the `sms:` protocol like this. Did you ever find a solution? We ended up just not permitting the `&` symbol in the sms body. – stereoscott Apr 10 '17 at 18:21

6 Answers6

5

This seems really crazy but encoding the body twice does the trick.

<a href="sms:?body=hello & stuff">not encoded (doesn't work)</a>
<a href="sms:?body=hello%20%26%20stuff">uri component encoded (doesn't work)</a>
<a href="sms:?body=hello%2520%2526%2520stuff">double uri component encoded (works fine)</a>

Working fiddle to test from an android device: https://jsfiddle.net/k96g2h48/

Crisman
  • 418
  • 1
  • 6
  • 19
5

Android and iOS respond to slightly different syntaxes. To put & inside the body of text, iOS needs URL encoding. For Android, it requires double encoding as mentioned in @Crisman's answer. check the below code:

<a href="sms:&body=hi%26bye">iOS</a>
<br>
<a href="sms:?body=hello%2520%2526%2520stuff">double uri component encoded (works fine)</a>

The first link worked in iOS and the second link works in Android. Notice the syntax of both URLs. & and ? with this they with & ios distinguish between number and body part whereas ? is used to separate number and body.

An example with number is like

<a href="sms:123456&body=hi%26bye">iOS</a>
<br>
<a href="sms:123456?body=hello%2520%2526%2520stuff">double uri component encoded (works fine)</a>

You can also try this fiddle

Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
1

Encode your & character because it has a special meaning in an URL ( it is the separator for fields)

<a href="sms:?body=This+is+the+message+with+%26+symbol">SMS</a>

The characters that have a special meaning in URL need to be ecnode if you just want there text respresentation.

wikipedia on percent encoding

rene
  • 41,474
  • 78
  • 114
  • 152
  • I tried this, messages from the & symbol onwards still goes missing. – Cirrus Sep 26 '13 at 09:12
  • On which browser are you trying this? I verfied this in Opera on win2k8 with Outlook as client (used mailto:?body=bar%26foo) and that works – rene Sep 26 '13 at 12:06
  • i'm using the phone to go onto the web app with android browser – Cirrus Sep 27 '13 at 05:28
  • are you sure the link doens't get double encoded? I don't have an android phone but can you do something like view source to verify that the html looks like you expect? – rene Sep 27 '13 at 06:46
0

Try using &#38; instead of "&" as this is the ASCII version of the character and used to show up as text in a HTML document

SaturnsEye
  • 6,297
  • 10
  • 46
  • 62
  • i just tried this, but still getting the same result. Any message from the & symbol onwards disappears. – Cirrus Sep 26 '13 at 08:15
0

I think you need to write like below

  <a href="sms:?body=This is the message with &amp; symbol">SMS</a>
Zainab
  • 25
  • 3
0

Have you try this char \u0026?

<a href="sms:?body=This is the message with \u0026 symbol">SMS</a>

reference from - https://stackoverflow.com/a/3705601/10989990

EDIT

Some browsers will accept & and some browsers will not accept this char so everything after & will be considered as the query parameter

EDIT

may be javascript helps to use

<!DOCTYPE html>
<html>
<body>
<script>
var txt = "SMS";
document.write("<p>" + txt.link("sms:?body=This is the message with & symbol") + "</p>");

</script>
</body>
</html>

Good Luck :)

Priyanka
  • 3,369
  • 1
  • 10
  • 33