Javascript退出&符"&"

mar*_*dge 1 html javascript escaping ampersand

我正在尝试使用此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;
}
Run Code Online (Sandbox Code Playgroud)

但是我的一些页面有一个&符号并且作为页面标题的一部分,并且该函数在其上扼流,并且仅在&之前插入文本而不是&之后的任何内容.(是的,"窒息"是一个技术性很强的术语.)

有没有办法逃避&所以JS不会窒息?&实际上显示&#064;在页面源中.或者我需要转到PHP功能?谢谢.

编辑:这有效: + encodeURIComponent(document.title); mail_str +=

M-f*_*ied 5

encodeURIComponent(document.title)
Run Code Online (Sandbox Code Playgroud)