将特殊字符传递给mailto正文会炸毁JavaScript [英] Passing special characters to mailto body blows up JavaScript
问题描述
我有一个C#ASP.NET应用程序,它为某些用户配置文件信息创建了一个JavaScript数组值。客户端,我使用jQuery / JavaScript读取数组并生成mailto链接。某些字段可以包含特殊字符,例如'& = / \
。
I have a C# ASP.NET app that creates a JavaScript array of values for some user profile information. Client-side, I use jQuery/JavaScript to read the array and generate a mailto link. Some of the fields can contain special characters, such as ' & = / \ "
.
这是C#代码:
private String generateElementsArray(){
StringBuilder sb = new StringBuilder();
sb.Append("var currentElements = { currentUserName: '");
sb.Append(currentUserName);
sb.Append("', currentUserEmail: '");
sb.Append(currentUserEmail);
sb.Append("', currentSite: '");
sb.Append(currentSite);
sb.Append("', currentTitle: '");
sb.Append(currentTitle);
sb.Append("'}");
return sb.ToString();
}
我写的以上方法对页面的值,生成此JavaScript:
I write the value of the above method to the page, which produces this JavaScript:
<script type="text/javascript">var currentElements = { currentUserName: 'Alex', currentUserEmail: 'myemailID', currentSite: 'Helpdesk', currentTitle: 'Phone User Guides & Troubleshooting'}</script>
然后我生成使用此JavaScript代码的电子邮件链接,将锚标记附加到页面上的元素:
Then I generate the email link using this JavaScript code, attaching the anchor tag to an element on the page:
function generateEmailTo(){
var body = currentElements.currentUserName + ' has shared a page with you on the intranet.%0A%0APage Title: %22' +
currentElements.currentTitle.replace("&","and") + '%22%0A' + $(location).attr('href').replace('#','');
var subject = currentElements.currentUserName + ' has shared a page with you on the intranet';
var mailto = 'mailto: ?body=' + body + '&subject=' + subject;
var anchor = '<a href="' + mailto + '"></a>';
$("#email-placeholder").wrap(anchor);
}
....
<img id="email-placeholder" title="Send this page to a friend." src="<%= baseUrl %>/SiteAssets/media/icons/email-icon.gif"/>
对于mailto正文,我注意到它只需要一小部分编码字符,例如%22
用于双引号,%0A
用于换行符。如何将单引号,&符号等特殊字符传递给mailto正文文本并保持JavaScript快乐?
For the mailto body text, I've noticed that it only takes a small set of the encoded characters, such as %22
for double-quotes, and %0A
for line breaks. How do I pass the special characters such as single quotes, ampersands, etc., to the mailto body text and keep JavaScript happy?
推荐答案
Mailto是一种URI方案,因此它的所有组件都必须进行URI编码。您可以使用JavaScript encodeURIComponent函数对mailto组件进行编码。请参阅以下示例:
Mailto is a URI scheme so all of its components must be URI encoded. You can use JavaScript encodeURIComponent function to encode mailto components. Please see the following example:
function buildMailTo(address, subject, body) {
var strMail = 'mailto:' + encodeURIComponent(address)
+ '?subject=' + encodeURIComponent(subject)
+ '&body=' + encodeURIComponent(body);
return strMail;
}
var strTest = buildMailTo('abc@xyz.com', 'Foo&foo', 'Bar\nBar');
/* strTest should be "mailto:abc%40xyz.com?subject=Foo%26foo&body=Bar%0ABar" */
window.open(strTest);
希望这个帮助。
这篇关于将特殊字符传递给mailto正文会炸毁JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!