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
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.