{"id":220,"date":"2012-06-23T16:24:17","date_gmt":"2012-06-23T08:24:17","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=220"},"modified":"2018-09-03T22:22:34","modified_gmt":"2018-09-03T14:22:34","slug":"how-to-ensure-that-your-javascript-is-able-to-write-unicode-characters-from-external-javascript-files-to-your-html-contents","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-ensure-that-your-javascript-is-able-to-write-unicode-characters-from-external-javascript-files-to-your-html-contents\/","title":{"rendered":"How to ensure that your Javascript is able to write unicode characters from external Javascript files to your HTML contents"},"content":{"rendered":"<p>Ever tried injecting Chinese characters into your HTML DOM from an external Javascript file but got something that resembles the following?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/ph\/img\/blog\/posts\/gibberish-characters-while-displaying-chinese-with-jQuery.jpg\" alt=\"Gibberish characters was shown when jQuery attempts to display error message in Chinese\"\/><\/p>\n<p>Well that was what I got for the Chinese version of my bilingual form initially as a result of <a href=\"http:\/\/www.techcoil.com\/blog\/how-to-validate-input-fields-as-the-user-is-filling-up-a-form-with-jquery\/\" title=\"How to validate input fields as the user is filling up a form with jQuery\" target=\"_blank\">validating an input field as the user is filling up my form<\/a>.<\/p>\n<p>In this post, I will discuss why I got the unwanted characters and what I did to solve the problem.<\/p>\n<h3>Segmentation of the Javascript codes<\/h3>\n<p>To facilitate the implementation of the form's validation logic, I had created one Javascript file for each language; one for the Chinese version and the other for the English version. Each Javascript file declares an object with the same set of members that are initialized with string literals that contains the error messages. <\/p>\n<p>With that, the Javascript codes for the bilingual form were segmented into three separate chunks:<\/p>\n<ol>\n<li>Codes that preset the object members with <strong>English<\/strong> string literals. These codes are contained in the <code>en-labels.js<\/code> file.\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlabelsMap = {\r\n    errMsgForBlankNumberField        : 'Please fill in a number.',\r\n    errMsgNoSpecialCharactersAllowed : 'Your input should be a number.',\r\n};\r\n<\/pre>\n<\/li>\n<li>Codes that preset the object members with <strong>Chinese<\/strong> string literals. These codes are contained in the <code>zh-simplified.js<\/code> file.\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlabelsMap = {\r\n    errMsgForBlankNumberField        : '\u8acb\u8f38\u5165\u53f7\u7801\u3002',\r\n    errMsgNoSpecialCharactersAllowed : '\u8acb\u52ff\u8f38\u5165\u7279\u6b8a\u5b57\u5143\u6216\u7b26\u865f\u3002',\r\n};\r\n<\/pre>\n<\/li>\n<li>Codes that validate the form input, which are contained in the <code>validation.js<\/code> file.\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$(document).ready(\r\n    \/\/ Handler for document ready event\r\n    function () {\r\n\r\n        $('#yourNumberTextField').change(\r\n \r\n            \/\/ Handler for changes detected on yourNumberTextField\r\n            function () {\r\n \r\n                if ($.trim( $(this).attr('value') ) == '') {\r\n                    $outputErrorMessage($(this),\r\n                        labelsMap.errMsgForBlankNumberField);\r\n                }\r\n                else if (!$.isNumeric($.trim($(this).attr('value')))) {\r\n                    $outputErrorMessage($(this),\r\n                        labelsMap.errMsgNoSpecialCharactersAllowed);\r\n                }\r\n                else {\r\n                    $(this).removeClass('errorField');\r\n                    $(this).next().remove();\r\n                }\r\n\r\n            } \/\/ end function()\r\n\r\n        ); \/\/ end $('#yourNumberTextField').change\r\n\r\n    } \/\/ end function\r\n); \/\/ end $(document).ready\r\n<\/pre>\n<\/li>\n<\/ol>\n<p>The server side code will then decide which Javascript file to include before the validation logic. <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;en-labels.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;validation.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>Or<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;zh-simplified-labels.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;validation.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>By separating the error messages from the validation logic, I need only one set of validation logic that will work for both versions the form. Furthermore, if the form needs to support another language in the future, I will just need to create another Javascript file with the respective error messages.<\/p>\n<h3>What had happened and how I solved the display problem<\/h4>\n<p>It turns out that the editor which I was using to write the contents of <code>zh-simplified-labels.js<\/code> had defaulted the file encoding to ANSI. To solve the display problem, I reopened <code>zh-simplified-labels.js<\/code> with my <a href=\"http:\/\/notepad-plus-plus.org\/\" title=\"Notepad++ homepage\" target=\"_blank\">Notepad++<\/a>, select the \"Convert to UTF-8\" option under the \"Encoding\" file menu, and save the changes. With that, my Chinese error message displays correctly.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/ph\/img\/blog\/posts\/correct-display-of-chinese-error-message-with-jQuery.jpg\" alt=\"Chinese error message correctly displayed.\"\/><\/p>\n<h3>Other things to take note<\/h3>\n<p>The following is a list of things to take note to ensure that Unicode characters are displayed correctly in your HTML page:<\/p>\n<ul>\n<li>The codes that renders the HTML document is saved with the UTF-8 encoding.<\/li>\n<li>The HTML content contains the following <code>meta<\/code> element in the HTML header section:\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;head&gt;\r\n    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=utf-8&quot;&gt;\r\n    &lt;!--Other head children--&gt;\r\n&lt;\/head&gt;\r\n<\/pre>\n<\/li>\n<\/ul>\n<h3>Other posts that you may want to read further<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/php-codes-to-tell-browsers-to-open-the-download-dialog-box-for-users-to-download-a-file\/\" title=\"PHP codes to tell browsers to open the download dialog box for users to download a file\" target=\"_blank\">PHP codes to tell browsers to open the download dialog box for users to download a file<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/handy-javascript-code-segments\/\" title=\"Handy Javascript code segments\" target=\"_blank\">Handy Javascript code segments<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/code-segments-for-rendering-html-4-0-pages\/\" title=\"Code segments for rendering html 4.0 pages\" target=\"_blank\">Code segments for rendering html 4.0 pages<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/how-to-send-http-post-requests-and-http-get-requests-using-jquery\/\" title=\"How to send HTTP post requests and HTTP get requests using jQuery\" target=\"_blank\">How to send HTTP post requests and HTTP get requests using jQuery<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/downloading-a-file-from-via-http-post-and-http-get-in-c\/\" title=\"Downloading a file via HTTP post and HTTP get in C#\" target=\"_blank\">Downloading a file via HTTP post and HTTP get in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/deciding-which-http-status-code-to-use-in-a-http-response\/\" title=\"Deciding which HTTP status code to use in a HTTP response\" target=\"_blank\">Deciding which HTTP status code to use in a HTTP response<\/a><\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-3y\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-3y&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-3y&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F220&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Ever tried injecting Chinese characters into your HTML DOM from an external Javascript file but got something that resembles the following?<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.techcoil.com\/ph\/img\/blog\/posts\/gibberish-characters-while-displaying-chinese-with-jQuery.jpg\" alt=\"Gibberish characters was shown when jQuery attempts to display error message in Chinese\"\/><\/p>\n<p>Well that was what I got for the Chinese version of my bilingual form initially as a result of <a href=\"http:\/\/www.techcoil.com\/blog\/how-to-validate-input-fields-as-the-user-is-filling-up-a-form-with-jquery\/\" title=\"How to validate input fields as the user is filling up a form with jQuery\" target=\"_blank\">validating an input field as the user is filling up my form<\/a>.<\/p>\n<p>In this post, I will discuss why I got the unwanted characters and what I did to solve the problem.<\/p>\n","protected":false},"author":1,"featured_media":1213,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[90,16,89,151],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/JavaScript-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-3y","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/220"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1213"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}