Encoding Json Data For A Mailto Link
How do I properly encode a mailto link with JSON data in the query parameters so that the link works as expected when some of the JSON data possibly includes spaces? Here is a simp
Solution 1:
You need to use encodeURIComponent
twice, because you are encoding a parameter inside another parameter.
Your link is using the mailto
protocol and using a body
parameter which content should be encoded. But, inside that content you are entering a URL which has parameters, so, this parameters should be encoded also.
Try this:
var data = {"Test": "Property with spaces"};
var mailTo = 'mailto:?body=' + encodeURIComponent('http://www.google.com/?body=' + encodeURIComponent(JSON.stringify(data)));
document.getElementById("link").href = mailTo;
<aid='link'>anchor</a>
Post a Comment for "Encoding Json Data For A Mailto Link"