1

我正在尝试使用此 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;
}

但是我的一些页面在页面标题中有一个 & 作为页面标题的一部分,并且该功能会阻塞,并且只在 & 之前插入文本,而不是 & 或之后的任何内容。(是的,“扼流圈”是一个技术性很强的术语。)

有没有办法逃避 & 让 JS 不会窒息?& 实际上出现&#064;在页面源中。还是我需要去一个 php 函数?谢谢。

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

4

3 回答 3

5
encodeURIComponent(document.title)
于 2011-01-27T20:02:04.733 回答
2

您应该在目标中插入的那些变量上调用encodeURIencodeURIComponent 。这将消除犯规角色并允许它通过。

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;
}
于 2011-01-27T20:01:50.700 回答
-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中使用\作为转义字符

于 2011-01-27T20:25:02.853 回答