我正在尝试使用此JS在客户端计算机上打开新电子邮件,页面标题已填充在主题行和正文中,由此链接调用<a href="javascript:mailpage()">Email</a>
function mailpage()
{ mail_str = "mailto:?subject= " + document.title; mail_str +=
"&body=Hi, I thought you might be interested in this: "
+ document.title; mail_str +=
". You can check out the web site at "
+ location.href; location.href = mail_str;
}
但是我的一些页面有一个&符号&amp;作为页面标题的一部分,并且函数会对此进行扼流,并且仅在&amp;之前插入文本。而不是&amp;或之后的任何事情(是的,“choke”是一个技术性很强的术语。)
有没有办法逃避&amp;所以JS不会窒息? &amp;实际上在页面源中显示为@
。或者我需要转到PHP功能?感谢。
修改:此方法有效: + encodeURIComponent(document.title); mail_str +=
答案 0 :(得分:5)
encodeURIComponent(document.title)
答案 1 :(得分:2)
您应该在目的地中插入的变量上调用encodeURI或encodeURIComponent。这将消除犯规字符并允许它通过。
function mailpage()
{
var subject = encodeURIComponent(document.title),
body= encodeURIComponent("Hi, I thought you might be interested in this: "
+ document.title + ". You can check out the web site at "
+ location.href;
location.href = "mailto:?subject= " +subject + "&body=" + body;
}
答案 2 :(得分:-1)
function mailpage()
{ mail_str = "mailto:?subject= " + document.title; mail_str +=
"\&body=Hi, I thought you might be interested in this: "
+ document.title; mail_str +=
". You can check out the web site at "
+ location.href; location.href = mail_str;
}
使用\作为javascript中的转义字符