user3378165 user3378165 -4 years ago 131
Javascript Question

Link gets cut of within a body of mailto

I have a javascript code that generates emails with links in the body.

Some of the links have spaces within the link, so the link gets cut of.

enter image description here

I tried replacing the spaces with the asacii code but it didn't work.

var lineBreak = "%0D%0A";

function GetMailToInfo(attachment, body) {

attachment = attachment ? attachment.replace(" ","%0D%0A") + lineBreak + lineBreak : "";
body += attachment + signature;
window.location.href = "mailto:" + emailTo + "?subject=" + subject() + "&body=" + body;

Any idea on how to solve that?

Answer Source

Use encodeURIComponent to properly escape special characters in URLs:

window.location.href = 
    "mailto:"   + encodeURIComponent(emailTo) + 
    "?subject=" + encodeURIComponent(subject()) + 
    "&body="    + encodeURIComponent(body);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download