问题描述
我使用表单中的值设置电子邮件正文
firstname = bob
lastname = dole
ebody ='名字:'+ firstname +'\r\\\
'+'姓氏:'+姓氏
window.location.href =' mailto:[email protected]?subject = test
email& body ='+ ebody;
如果我做了一个alert(ebody);我在firstname和amp;
姓氏,但是当它打开outlook时,整个ebody字符串
在电子邮件正文中不会出现换行符。
我试过了\也。有没有什么东西可以让一条线
休息?
提前致谢 //tools.ietf.org/html/rfc2368rel =noreferrer> RFC 2368 指出,mailto正文内容必须是URL编码的,使用%-escaped形式表示通常会在URL。这些字符包括空格和(如2368年第5节明确指出的那样)CR和LF。您可以通过编写
ebody ='First%20Name:%20'+ firstname +'%0D%0A'+'Last%20Name:%20'+ lastname;
但让JavaScript逃离你更容易和更好,就像这样:
ebody ='名字:'+ firstname +'\r\\\
'+'姓氏:'+ lastname;
ebody = encodeURIComponent(ebody);
这不仅可以让您不必识别和查找需要的字符的十六进制值在您的固定文本中进行编码,它还会编码 firstname
和 lastname
变量中的任何高贱字符。
I'm setting the body of an email using values from a form
firstname = bob
lastname = dole
ebody = 'First Name: ' + firstname + '\r\n' + 'Last Name: ' + lastname
window.location.href = 'mailto:[email protected]?subject=test
email&body=' + ebody;
If I do an "alert(ebody);" I get the linebreak between firstname &lastname, however when it opens up outlook, the entire ebody stringappears without a linebreak in the email body.
I've tried just \n also. is there something that can give be a linebreak?
Thanks in advance
RFC 2368 says that mailto body content must be URL-encoded, using the %-escaped form for characters that would normally be encoded in a URL. Those characters includes spaces and (as called out explicitly in section 5 of 2368) CR and LF.
You could do this by writing
ebody = 'First%20Name:%20' + firstname + '%0D%0A' + 'Last%20Name:%20' + lastname;
but it's easier and better to have JavaScript do the escaping for you, like this:
ebody = 'First Name: ' + firstname + '\r\n' + 'Last Name: ' + lastname;
ebody = encodeURIComponent(ebody);
Not only will that save you from having to identify and look up the hex values of characters that need to be encoded in your fixed text, it will also encode any goofy characters in the firstname
and lastname
variables.
这篇关于Javascript在mailto正文中添加linebreak的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!