{"id":127,"date":"2009-10-30T15:45:16","date_gmt":"2009-10-30T21:45:16","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=127"},"modified":"2011-03-16T16:36:47","modified_gmt":"2011-03-16T22:36:47","slug":"javascript-tips-convert-string-to-unicode-values","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2009\/10\/javascript-tips-convert-string-to-unicode-values\/","title":{"rendered":"Javascript Tip: Convert string to unicode values"},"content":{"rendered":"<p>The following script will convert a string to the unicode values using javascript.<br \/>\ntoUnicode is a extension to String.charCodeAt. The benefit of this function is that it returns the whole string a html unicode format.<\/p>\n<p><b>Code<\/b><\/p>\n<pre lang='javascript' line='1'>\r\nString.prototype.toUnicode = function () {\r\n    var uni = [],\r\n        i = this.length;\r\n    while (i--) {\r\n        uni[i] = this.charCodeAt(i);\r\n    }\r\n    return \"&#\" + uni.join(';&#') + \";\";\r\n};\r\n<\/pre>\n<p>Here\u2019s another method that I thought of.<br \/>\nThis one is using a regexp instead of a while loop and an array to form the unicode text.<\/p>\n<pre lang='javascript' line='1'>\r\nString.prototype.toUnicode = function () {\r\n    return this.replace(\/.\/g, function (char) {\r\n        return \"&#\" + String.charCodeAt(char) + \";\";\r\n    });\r\n};\r\n<\/pre>\n<p><b>Usage:<\/b><br \/>\nCall toUnicode() on any string object.<\/p>\n<pre lang=\"javascript\" line=\"1\">obj.toUnicode();<\/pre>\n<p><b>Example:<\/b><\/p>\n<pre lang=\"javascript\" line='1'>\u201c<li\/>\u201d.toUnicode() \/\/outputs: &#60;&#108;&#105;&#47;&#62;<\/pre>\n<p>However, if possible use document.createTextNode to insert text instead of converting it to unicode. Document.createTextNode is a native DOM method meaning it&#8217;s a lot faster, and easier to understand .<br \/>\nMore infor here: <a href=\"https:\/\/developer.mozilla.org\/en\/DOM\/document.createTextNode\">document.createTextNode<\/a><\/p>\n<p><iframe src=\"http:\/\/rcm.amazon.com\/e\/cm?t=baterucom-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=0672322013&#038;ref=tf_til&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following script will convert a string to the unicode values using javascript. toUnicode is a extension to String.charCodeAt. The benefit of this function is that it returns the whole string a html unicode format. Code String.prototype.toUnicode = function () { var uni = [], i = this.length; while (i&#8211;) { uni[i] = this.charCodeAt(i); } &hellip; <a href=\"https:\/\/bateru.com\/news\/2009\/10\/javascript-tips-convert-string-to-unicode-values\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Javascript Tip: Convert string to unicode values<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,10],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-tutorials"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":20,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/127\/revisions\/130"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}