1

I have an object literal

var object = {
    test: "test";
}

I call JSON.stringify on this object and append it to a query string

http://example.com?parameter={"test":"test"}

But when I append this query string into the mailto body, only this part

http://example.com?parameter=

got wrapped as an hyperlink while the rest is showed as plain text.

what I did was this:

window.location = "mailto:someone@example.com?subject=subject&body=http://example.com?parameter={"test":"test"}";

And when the email client view showed, only the part before '=' got wrapped as hyperlink.

Frank Tian
  • 787
  • 3
  • 8
  • 22

2 Answers2

3

The application that renders the emails just doesn't consider { as part of a url, you can try encoding it. It wont look pretty though.
Something like

window.location = 'mailto:someone@example.com?subject=subject&body=' + encodeURIComponent('http://example.com?parameter=' + encodeURIComponent('{"test":"test"}'));
Musa
  • 96,336
  • 17
  • 118
  • 137
2

This is actually related to accepted characters on URL. You should check this answer here: Characters allowed in a URL

So your link will work if you change this " to this ':

window.location = "mailto:someone@example.com?subject=subject&body=http://example.com?parameter={'test':'test'}";

Check out this Fiddle to replace quotation marks

Community
  • 1
  • 1
jmartins
  • 991
  • 4
  • 16
  • That's not it, it's the `?` and `=` in the `body` parameter. Can't parse a query string without those being encoded. – Brad Christie Feb 26 '15 at 22:04
  • 1
    @BradChristie works fine for me in multiple browsers – charlietfl Feb 26 '15 at 22:08
  • @BradChristie Actually, now they are automatically encoded by the browser. Try it on chrome. `mailto:someone@example.com?subject=subject&body=http://example.com?parameter={'test':'test'}` – jmartins Feb 26 '15 at 22:10
  • 1
    @charlietfl Exactly. But here, the problem is only this quotation mark "". Because it is not encoded by the browser. – jmartins Feb 26 '15 at 22:12
  • 1
    So relying on the browser to fix the problem for you instead of following the age old protocol is the answer. ... doesn't seem like right answer. – Brad Christie Feb 26 '15 at 23:03
  • It isn't the best. That's why I voted up for the other solution, it's better with encodeURIComponent – jmartins Feb 26 '15 at 23:19