Notes on Java, Solaris, PHP, LDAP…

September 11, 2008

Generating Unicode email via mailto: link in Javascript

Filed under: Java — negev @ 11:28 am
Tags:

I wanted to generate mailto: link with a subject and body which contained Unicode characters. As an example for Unicode letters I was getting following from a PHP form. The application workflow required that I process any transformation in Javascript rather than on server.
%uB9E4%uB274%uC5BC

That stands for nice Korean 매뉴얼:
javascript:alert( unescape(‘%uB9E4%uB274%uC5BC’) )

which in HTML Unicode escapes is (following without spaces, otherwise WordPress shows them as Korean letters):
& #xB9E4;& #xB274;& #xC5BC;

javascript:alert( ‘%EB%A7%A4%EB%89%B4%EC%96%BC’ )
-> same Korean letters

javascript:alert( escape(‘%EB%A7%A4%EB%89%B4%EC%96%BC’ ) )
-> %uB9E4%uB274%uC5BC

javascript:alert( encodeURI(‘매뉴얼’ ) )
-> %EB%A7%A4%EB%89%B4%EC%96%BC

So my final transformation was:
var encodedSubject= encodeURI( unescape( ‘%uB9E4%uB274%uC5BC’) );
var encodedBody= encodeURI( unescape( ‘%uB9E4%uB274%uC5BC Lala lala’) );
javascript:window.location.href= ‘mailto:?subject=’ +encodedSubject;+ ‘&body=’ +encodedBody;
————————————————————–

I’ve also tried following to  generate HTML escapes of Unicode characters, but it doesn’t work for URLs:

function url_encoded_unicode_to_html( url ) {
var regex_search= new RegExp( “%u([0-9a-fA-F]{4,4})”, “g” );
var regex_replace= ‘&#x$1;’;

return url.replace( regex_search, regex_replace ); //( ‘/%u([0-9a-fA-F]{4,4})/’, ‘&#x$1;’, $text );
}

Advertisements

3 Comments »

  1. Did you get this to work? I’m trying to create a mailto link that uses Chinese unicode characters in the subject and body links. Please let me know if you had any luck with that.

    Thanks,
    D.

    Comment by Don — March 25, 2009 @ 3:39 am | Reply

    • This variant seems to work for Japanese, so presumably also Chinese:

      function escapeAndUriEncode(val) {
      /// Escape the string, then fix escaped Unicode references to

      var escapedVal = escape(val);
      var unicodeFixed = escapedVal.replace(/\%u([0-9A-F]{4})/g, function (match) {
      return encodeURI(unescape(match));
      });
      return unicodeFixed;
      }

      Comment by Andy Tuba — June 18, 2012 @ 8:32 pm | Reply

  2. I visit each day a few websites and blogs to read posts,
    however this blog offers quality based writing.

    Comment by tampines new ec — October 22, 2012 @ 7:01 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: