Replacement for javascript escape?

I know the escape function is deprecated and that you should use encodeURI or encodeURIComponent instead. However, encodeUri and encodeUriComponent do not perform the same function as escape.

I want to create a post link in javascript with swedish language. Here is a comparison between escape, encodeURIComponent and encodeURI:

console.log("mailto:?subject="+escape(subject)+"&body=" + escape(body));
console.log("mailto:?subject="+encodeURIComponent(subject)+"&body=" + encodeURIComponent(body));
console.log("mailto:?subject="+encodeURI(subject)+"&body=" + encodeURI(body));  



Only the mailto link created with escape opens properly formatted mail in Outlook using IE or Chrome. When using encodeURI or encodeURIComponent, the subject says:

My subject with åäö


and the body also looks spoiled.

Is there any other function besides escape that I can use to get a working mailto link?


source to share

2 answers


is defined in section B.2.1.2 escape , and the introductory text of Appendix B reads:

... All language features and behaviors specified in this appendix have one or more undesirable characteristics, and in the absence of obsolete use they will be removed from this specification ....

For symbols, the value of which is equal to the unit code 0xFF or less, escape()

creates a two-digit escape-sequence %xx

. This basically means it escape()

converts a string containing only characters from U+0000

to U+00FF

to a percentage-encoded string using latin-1 encoding.

For characters with a larger code unit, a four-digit format is used %uxxxx

. This is not valid in the hfields

(where object and body are stored) mailto:

-URI section (as defined in RFC6068 ):

mailtoURI    = "mailto:" [ to ] [ hfields ]
to           = addr-spec *("," addr-spec )
hfields      = "?" hfield *( "&" hfield )
hfield       = hfname "=" hfvalue
hfname       = *qchar
hfvalue      = *qchar
qchar        = unreserved / pct-encoded / some-delims
some-delims  = "!" / "$" / "'" / "(" / ")" / "*"
               / "+" / "," / ";" / ":" / "@"



and pct-encoded

defined in STD66 :

unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded   = "%" HEXDIG HEXDIG


A percent sign is only allowed if it is immediately followed by two hexadecimal digits, and a percentage followed by is u

not allowed.

Using a self-implemented version that behaves exactly the same escape

doesn't solve anything - instead, just keep using it escape

, it won't be removed anytime soon.

For summer: your previous use escape()

generated latin 1 percent post characters in -URI if all characters are in the range from U+0000

to U+00FF

, otherwise an invalid URI was generated (which can still be interpreted correctly by some applications if they had in mind javascript encoding / decoding compatibility).

More correctly (without the risk of generating invalid URI) and promising to generate interest UTF8-mail to -URI using encodeURIComponent()

(do not use encodeURI()

, it will not escape ?

, /

...). RFC6068 requires the use of UTF-8 in many places (but allows other encodings for "MIME-encoded words and bodies in composite e-mails").


text_latin1="Swedish åäö"
text_other="Emoji 😎"


function mime_word(text){
  q_encoded = encodeURIComponent(text) //to utf8 percent encoded
  .replace(/[_!'()*]/g, function(c){return '%'+c.charCodeAt(0).toString(16).toUpperCase();})// encode some more chars as utf8
  .replace(/%20/g,'_') // mime Q-encoding is using underscore as space
  .replace(/%/g,'='); //mime Q-encoding uses equal instead of percent
  return encodeURIComponent('=?utf-8?Q?'+q_encoded+'?=');//add mime word stuff and escape for uri

//don't use mime_word for body!!!

<a id="escape-latin-1-link">escape()-latin1</a><br/>
<a id="escape-other-chars-link">escape()-emoji</a><br/>
<a id="utf8-link">utf8</a><br/>
<a id="utf8-other-chars-link">utf8-emoji</a><br/>
<a id="mime-word-link">mime-word</a><br/>
<a id="mime-word-other-chars-link">mime-word-emoji</a><br/>

Run codeHide result

For me, UTF-8 links and Mime-Word links work in Thunderbird. Only inline UTF-8 links work in Windows 10 built-in Mailapp and in my latest Outlook.



The escape () function is deprecated in JavaScript version 1.5. Use encodeURI () or encodeURIComponent () instead .


string:            "May/June 2016, Volume 72, Issue 3"
escape:            "May/June%202016%2C%20Volume%2072%2C%20Issue%203"
encodeURI:         "May/June%202016,%20Volume%2072,%20Issue%203"





All Articles