我目前正在开发一个网络应用程序,有这部分代码,我必须在短信框中预先填充消息。所以我的代码看起来像这样:
<a href="sms:?body=This is the message with & symbol">SMS</a>
Run Code Online (Sandbox Code Playgroud)
在预先填充的消息中,从 & 符号开始的所有内容都不会出现在电话的消息框中。我知道我必须对其进行编码,但我不知道编码代码是什么。有什么解决办法吗?谢谢。
这看起来真的很疯狂,但是对主体进行两次编码就可以了。
<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>
Run Code Online (Sandbox Code Playgroud)
从 Android 设备进行测试的工作小提琴:https://jsfiddle.net/k96g2h48/
Android 和 iOS 对语法的响应略有不同。为了放入& 文本正文中,iOS 需要 URL 编码。对于 Android,它需要双重编码,如 @Crisman 的回答中所述。检查下面的代码:
<a href="sms:&body=hi%26bye">iOS</a>
<br>
<a href="sms:?body=hello%2520%2526%2520stuff">double uri component encoded (works fine)</a>Run Code Online (Sandbox Code Playgroud)
第一个链接适用于 iOS,第二个链接适用于 Android。请注意两个 URL 的语法。&通过?这个,他们在&ios 中区分数字和正文部分,而?用于分隔数字和正文。
一个带有数字的例子就像
<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>Run Code Online (Sandbox Code Playgroud)
你也可以尝试这个小提琴
| 归档时间: |
|
| 查看次数: |
3729 次 |
| 最近记录: |