mailto中的Html超链接正文选项不支持一些特殊字符,如'&'。

问题描述 投票:0回答:1

我使用超链接来提供mailto的功能,我动态的为邮件创建body,body的代码为

let value = 50;
let titlevar = stack overflow example
let myLink = "http://localhost:5500/abc/def.html?did=" + value + "&title=" + titlevar
let emailstring = `mailto:?Subject=Sometext&body=Click here to view my example:  %0D ` + myLink

我用一个标签作为。

<a href="` + emailstring + `">

但在打开这个链接时,在outlook中,字符串被打破。我只得到这个。http:/localhost:5500abcdef.html?did=50。

我觉得Outlook是限制性的 '&' 这个符号,所以我试着用逗号代替它(','),它是按照预期工作的

而我有空间在 titlevar 所以我已经尝试 encodeURI(titlevar) 这回 stack%20overflow%20example. 这在前景上也不支持。它是在 http:/localhost:5500abcdef.html?did=50,title=stack。.

这是不工作的Outlook,我没有尝试与Gmail的。

hyperlink outlook special-characters mailto
1个回答
0
投票

你需要在后面对整个url值进行编码。Subject=encodeURIComponent.

如果主题包含HTML和URL本身,那么这些也需要使用encodeURI进行编码。是的,这意味着某些部分需要使用两层 encodeURIComponent。

const myLink = "http://localhost:5500/abc/def.html?did=" + value + "&title=" + encodeURIComponent(titlevar);
const body = encodeURIComponent('Click here to view my example: ' + myLink);
const emailstring = `mailto:?Subject=Sometext&body=${body}`;

一般的问题是,当你把字符串嵌入到一个url中时,你应该总是对它们进行编码。如果该编码的字符串被嵌入到 另一个 url,也进行编码。

© www.soinside.com 2019 - 2024. All rights reserved.