{"id":53,"date":"2008-04-20T15:34:01","date_gmt":"2008-04-20T12:34:01","guid":{"rendered":"http:\/\/www.held.org.il\/blog\/?p=53"},"modified":"2012-05-21T02:39:18","modified_gmt":"2012-05-20T23:39:18","slug":"javascript-encodeuri","status":"publish","type":"post","link":"http:\/\/www.held.org.il\/blog\/2008\/04\/javascript-encodeuri\/","title":{"rendered":"JavaScript: encodeURI()"},"content":{"rendered":"<p>Internationalization is no simple matter<span style=\"color: #888888;\"><sup>tm<\/sup><\/span><\/p>\n<p><span style=\"color: #808080;\"><strong>JS URL Encoding<\/strong><\/span><\/p>\n<p>Sometimes we want to pass a free-text user-input string in the URL, which may be at any language. At work, we used the <em>escape()<\/em> JS built-in function to make a UTF-8 legal URL. <span style=\"color: #ff0000;\">*WRONG*<\/span><\/p>\n<p><!--more--> Problems arised when we added the Italian language; the egrave character (\u00c3\u00a8) isn't translated to a utf-8 representation (\"%C3%A8\") but to something else (\"%E8\"). Some <a href=\"http:\/\/xkr.us\/articles\/javascript\/encode-compare\/\">reading<\/a> led me to the conclusion:<\/p>\n<p><strong><em>escape()<\/em> may be deprecated, but surely is BAD for URL encoding<\/strong><\/p>\n<p>We should've used<em> <a title=\"encodeURI ref\" href=\"http:\/\/developer.mozilla.org\/en\/docs\/Core_JavaScript_1.5_Reference:Global_Functions:encodeURI\">encodeURI()<\/a><\/em>, or <a title=\"encodeURIComponent ref\" href=\"http:\/\/developer.mozilla.org\/en\/docs\/Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent\"><em>encodeURIComponent()<\/em><\/a>. The former is good for a whole URL, thus keeping the url-reserved-chars such as \"&amp;\", the latter is good for a specific URL component thus also encoding the \"&amp;\".<\/p>\n<p><span style=\"color: #808080;\"><strong>A similar PHP issue<\/strong><\/span><\/p>\n<p>Similarly, PHP <a title=\"htmlentities from php.net\" href=\"http:\/\/www.php.net\/htmlentities\"><em>htmlentities()<\/em><\/a> translates \"\u00c3\u00a8\" to \"&egrave ;\", while <a title=\"htmlspecialchars from php.net\" href=\"http:\/\/www.php.net\/htmlspecialchars\">htmlspecialchars()<\/a> keeps it in its utf-8 form. The former says \"convert ANYTHING that has an html equivalent\", the latter says \"convert only special chars\".<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Internationalization is no simple mattertm JS URL Encoding Sometimes we want to pass a free-text user-input string in the URL, which may be at any language. At work, we used the escape() JS built-in function to make a UTF-8 legal URL. *WRONG*<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[58,68,87],"_links":{"self":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts\/53"}],"collection":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":0,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}