user930514
user930514

Reputation: 927

encodeURIcomponent while constructing mailto link

When i use encodeURIComponent to encode the body as shown below, and if the body has spaces in it, the resulting link is broken and appears as follows :

http://www.sample.com/sample/explore?itemId=123&type=sample&name=name with space

body = "http://www.sample.com/sample/explore?itemId=123&type=sample&name=name with space"

var newLink = "mailto:?subject=" + encodeURIComponent(subject) + "&body=" + encodeURIComponent(body);

window.location = newLin

How can i fix it and create a valid url?

console.log shows the encoded url correctly but in outlook the link is broken. It should appear as a proper hyperlink

Upvotes: 2

Views: 2730

Answers (1)

Riccardo Volpe
Riccardo Volpe

Reputation: 1633

Have you tried to replace space with %20 in the body variable?

body = "http://www.sample.com/sample/explore?itemId=123&type=sample&name=name%20with%20space"

I suggest you to use this tool to encode plain text into querystring. If you need to pass a newline (\n), for example, use the %0A for newline \n and %0D for carriage return \r . Look at this ASCII table to understand the encoding: the second column (Hx = hexadecimal ASCII representation), with a % as prefix, corresponds to the red character reported in the fifth column (Chr = character), that is, for example:

space character (fifth column, Chr, in the table) = %20 (second column, Hx, in the table) ≅ + (read this answer)

Upvotes: 2

Related Questions