2

I tried like this when handling chinese, but the output is messy code garbled:

var a = "你好";
undefined
a
"你好"
a = unescape(encodeURIComponent(a));
"ä½ å¥½"
a
"ä½ å¥½"

compared to handling English:

var a = "Hello";
undefined
a
"Hello"
a = unescape(encodeURIComponent(a));
"Hello"

Here is my whole code:

var content = $("div#test").html();
content = unescape(encodeURIComponent( content ));
content = window.btoa(content);
content = "data:image/svg+xml;filename:{{ request.session.access_token.uid }}.svg;base64," + content;

UPDATED:

What I am trying to do is encoding a generated SVG as data url for user to download. I solved this problem by converting SVG to canvas (using CanVG) first and use html2canvas to generate base64 ... finally it works ...

10
  • What is your input and what do you need as output?
    – ATOzTOA
    Jan 26, 2013 at 9:23
  • how is your output related to the code? Jan 26, 2013 at 9:24
  • What do you mean by "the output is messy code"?
    – user663031
    Jan 26, 2013 at 10:01
  • 1
    Try roundtripping this, and you'll see it works fine: decodeURIComponent(escape(unescape(encodeURIComponent("東京")))) "東京", even thought the intermediate result looks like funny characters--because it IS funny characters.
    – user663031
    Jan 26, 2013 at 10:27

1 Answer 1

4

Try roundtripping this, and it works fine:

decodeURIComponent(escape(window.atob(window.btoa(unescape(encodeURIComponent("‌​東京"))))))

Whether or not some intermediate result appears to be garbled is not relevant.

The problem with the window.btoa(decodeURIComponent(escape(unescape(encodeURIComponent("東京"))))) in one of the comments is the calls are not balanced and not in the right order.

2
  • You answer do run withou error in Chrome. But what I want is base64 provided by window.btoa(unescape(encodeURIComponent("‌​東京")), which is not recognize correctly in Chrome when opening base64 data URL.
    – jo32
    Jan 28, 2013 at 11:48
  • thanks this seems to be the simplest solution ive found so far. im going to try it out some more. jsfiddle.net/gfjsycqd
    – ColacX
    Jun 26, 2015 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.