javascript中的超链接,用于邮件主题

时间:2014-12-28 21:00:05

标签: javascript html

我有js代码在按下链接时打开mailto对话框,它正在“与朋友分享”功能:

setTimeout( function(){
            var subject, body, email_string;
            subject = oScript_x.post_title;

            body = "you got mail from";
            //body += "link: " + "<a href='" + location.href + "'>" + location.href + "  </a>";
            body += "link " + location.href;


            email_string = "mailto:?subject=" + subject + "&body=" + body;
            email_string = email_string.replace(/ /g, "%20" ).replace(/\n/g, "%0A");

我试过用这个:     body + =“link:”+“”+ location.href +“”; 但没有运气......

所以现在我只将链接显示为没有链接的文本。 我很感激帮助让链接可点击并在锚文本下。

由于

1 个答案:

答案 0 :(得分:2)

问题在于你没有正确地逃避事情。您应该对添加的每个参数使用encodeURIComponent,而不是手动替换某些模式。

换句话说,您的代码应如下所示:

var subject = ...  // whatever you do to create this string
var body = ... // whatever you do to create this string
var encodedSubject = encodeURIComponent(subject);
var encodedBody = encodeURIComponent(body);
var emailLink = 'mailto:?subject=' + encodedSubject + '&body=' + encodedBody;
// ... use emailLink