逃脱&用于在邮件客户端中显示(mailto 链接)
我有一个像这样的 JavaScript 函数:
var strBody = encodeURI(window.location.href);
var strSubject = encodeURI(document.title);
var mailto_link = "mailto:?subject=" + encodeURI(strSubject) + "&body=" + strBody;
此代码在超链接的 onclick 事件上执行,并打开邮件客户端 (mailto://)。然而,页面标题有几个&符号,但标题仅在第一个 & 之前被选取。该 url 始终会被获取。
转义 & 的正确 JavasSript 是什么?并将其显示在邮件客户端的主题行中?
I have a JavaScript function like so:
var strBody = encodeURI(window.location.href);
var strSubject = encodeURI(document.title);
var mailto_link = "mailto:?subject=" + encodeURI(strSubject) + "&body=" + strBody;
This code is executed on a hyperlink's onclick event, and opens the mail client (mailto://). However, the title of the page has several & symbols, but the title is only picked up until the first &. The url is always picked up.
What is the correct JavasSript to escape the & and display it in the mail client's subject line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该这样做(
encodeURIComponent
而不是encodeURI
)。在您的原始代码中,您还错误地对主题进行了双重编码(一次在第2行,然后再次在第3行)。
我冒昧地重命名了您的变量,以便更清楚地表明它们包含编码的主题和正文,而不是原始文本。
should do it (
encodeURIComponent
instead ofencodeURI
).In your original code you were also incorrectly double encoding the subject (once on line 2, and then again on line 3).
I took the liberty of renaming your variables to make it clearer that they contain the encoded subject and body, as opposed to the original text.
您想要 encodeURIComponent 而不是encodeURI。
You want encodeURIComponent not encodeURI.