pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<tfoot id='Bl6Fk'></tfoot>
    • <bdo id='Bl6Fk'></bdo><ul id='Bl6Fk'></ul>
    <legend id='Bl6Fk'><style id='Bl6Fk'><dir id='Bl6Fk'><q id='Bl6Fk'></q></dir></style></legend>

      1. <small id='Bl6Fk'></small><noframes id='Bl6Fk'>

        <i id='Bl6Fk'><tr id='Bl6Fk'><dt id='Bl6Fk'><q id='Bl6Fk'><span id='Bl6Fk'><b id='Bl6Fk'><form id='Bl6Fk'><ins id='Bl6Fk'></ins><ul id='Bl6Fk'></ul><sub id='Bl6Fk'></sub></form><legend id='Bl6Fk'></legend><bdo id='Bl6Fk'><pre id='Bl6Fk'><center id='Bl6Fk'></center></pre></bdo></b><th id='Bl6Fk'></th></span></q></dt></tr></i><div class="yuq5ruw" id='Bl6Fk'><tfoot id='Bl6Fk'></tfoot><dl id='Bl6Fk'><fieldset id='Bl6Fk'></fieldset></dl></div>

        如何在Javascript中獲取日文字符的長度?

        How to get the length of Japanese characters in Javascript?(如何在Javascript中獲取日文字符的長度?)

            • <bdo id='lAVNa'></bdo><ul id='lAVNa'></ul>

              <legend id='lAVNa'><style id='lAVNa'><dir id='lAVNa'><q id='lAVNa'></q></dir></style></legend>
              <i id='lAVNa'><tr id='lAVNa'><dt id='lAVNa'><q id='lAVNa'><span id='lAVNa'><b id='lAVNa'><form id='lAVNa'><ins id='lAVNa'></ins><ul id='lAVNa'></ul><sub id='lAVNa'></sub></form><legend id='lAVNa'></legend><bdo id='lAVNa'><pre id='lAVNa'><center id='lAVNa'></center></pre></bdo></b><th id='lAVNa'></th></span></q></dt></tr></i><div class="nycu0xa" id='lAVNa'><tfoot id='lAVNa'></tfoot><dl id='lAVNa'><fieldset id='lAVNa'></fieldset></dl></div>
                1. <small id='lAVNa'></small><noframes id='lAVNa'>

                2. <tfoot id='lAVNa'></tfoot>
                    <tbody id='lAVNa'></tbody>
                  本文介紹了如何在Javascript中獲取日文字符的長度?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有一個帶有 SHIFT_JIS 字符集的 ASP 經典頁面.頁面head部分下的meta標簽是這樣的:

                  I have an ASP Classic page with SHIFT_JIS charset. The meta tag under the page's head section is like this:

                  <meta http-equiv="Content-Type" content="text/html; charset=shift_jis">
                  

                  我的頁面有一個文本框 (txtName),它只能允許 200 個字符.我有一個驗證字符長度的 Javascript 函數,該函數在我的提交按鈕的 onclick() 事件中調用.

                  My page has a text box (txtName) that should only allow 200 characters. I have a Javascript function that validates the character length, which is called on the onclick() event of my Submit button.

                  if(document.frmPage.txtName.value.length > 200) {
                    alert("You have exceeded the maximum length of 200.");
                    return false;
                  }
                  

                  問題是,Javascript 無法獲取以 SHIFT_JIS 編碼的正確長度的日文字符.例如,字符測的 SHIFT_JIS 長度為 8 個字符,但 Javascript 僅將其識別為一個字符,可能是因為 Javascript 默認使用的 Unicode 編碼.在 SHIFT_JIS 中,某些字符(例如 ケ)有 2 或 3 個字符.

                  The problem is, Javascript is not getting the correct length of Japanese character encoded in SHIFT_JIS. For example, the character 測 has a SHIFT_JIS length of 8 characters, but Javascript is only recognizing it as one character, probably because of the Unicode encoding that Javascript uses by default. Some characters like ケ have 2 or 3 characters when in SHIFT_JIS.

                  如果我只依賴 Javascript 提供的長度,長日文字符將通過頁面驗證并嘗試保存在數據庫中,然后由于 DB 列的最大長度為 200 而失敗.

                  If I will only depend on the length provided by Javascript, long Japanese characters would pass the page validation and it will try to save on the database, which will then fail because of the 200 maximum length of the DB column.

                  我使用的瀏覽器是 Internet Explorer.有沒有辦法使用 Javascript 獲取日文字符的 SHIFT_JIS 長度?是否可以使用 Javascript 從 Unicode 轉換為 SHIFT_JIS?如何?

                  The browser that I'm using is Internet Explorer. Is there a way to get the SHIFT_JIS length of the Japanese character using Javascript? Is it possible to convert from Unicode to SHIFT_JIS using Javascript? How?

                  感謝您的幫助!

                  推薦答案

                  例如,字符測的 SHIFT_JIS 長度為 8 個字符,但 Javascript 僅將其識別為一個字符,可能是因為 Unicode 編碼的原因

                  For example, the character 測 has a SHIFT_JIS length of 8 characters, but Javascript is only recognizing it as one character, probably because of the Unicode encoding

                  讓我們明確一點:測,U+6D4B(漢字'測量,估計,猜想')單個字符.當您將其編碼為特定編碼(如 Shift-JIS)時,它很可能會變成多個 字節.

                  Let's be clear: 測, U+6D4B (Han Character 'measure, estimate, conjecture') is a single character. When you encode it to a particular encoding like Shift-JIS, it may very well become multiple bytes.

                  一般而言,JavaScript 不提供編碼表,因此您無法確定一個字符將占用多少字節.如果你真的需要,你必須攜帶足夠的數據來自己解決.例如,如果您假設輸入僅包含在 Shift-JIS 中有效的字符,則此函數將通過保留所有單字節字符的列表來計算需要多少字節,并假設每個其他字符占用兩個字節:

                  In general JavaScript doesn't make encoding tables available so you can't find out how many bytes a character will take up. If you really need to, you have to carry around enough data to work it out yourself. For example, if you assume that the input contains only characters that are valid in Shift-JIS, this function would work out how many bytes are needed by keeping a list of all the characters that are a single byte, and assuming every other character takes two bytes:

                  function getShiftJISByteLength(s) {
                      return s.replace(/[^x00-x80?????????????????????????????????????????????????????????????? ? ????]/g, 'xx').length;
                  }
                  

                  但是,Shift-JIS 中沒有 8 字節序列,而且 Shift-JIS 中根本沒有字符測".(這是一個在日本不使用的漢字.)

                  However, there are no 8-byte sequences in Shift-JIS, and the character 測 is not available in Shift-JIS at all. (It's a Chinese character not used in Japan.)

                  你可能會認為它構成一個 8 字節序列的原因是:當瀏覽器無法在表單中提交字符時,因為它不存在于目標字符集中,它會用 HTML 字符引用替換它:在這種情況下 &#27979;.這是一個有損的修改:您無法分辨用戶是按字面輸入的 還是 &#27979;.如果您將提交的內容 &#27979; 顯示為 那么這意味著您忘記對輸出進行 HTML 編碼,這可能意味著您的應用程序很容易受到攻擊跨站點腳本.

                  Why you might be thinking it constitutes an 8-byte sequence is this: when a browser can't submit a character in a form, because it does not exist in the target charset, it replaces it with an HTML character reference: in this case &#27979;. This is a lossy mangling: you can't tell whether the user typed literally or &#27979;. And if you are displaying the submitted content &#27979; as then that means you are forgetting to HTML-encode your output, which probably means your application is highly vulnerable to cross-site scripting.

                  唯一明智的答案是使用 UTF-8 而不是 Shift-JIS.UTF-8 可以愉快地對 測 或任何其他字符進行編碼,而無需求助于損壞的 HTML 字符引用.如果您需要在數據庫中存儲受編碼字節長度限制的內容,可以使用一種偷偷摸摸的技巧來獲取字符串中 UTF-8 字節的數量:

                  The only sensible answer is to use UTF-8 instead of Shift-JIS. UTF-8 can happily encode 測, or any other character, without having to resort to broken HTML character references. If you need to store content limited by encoded byte length in your database, there is a sneaky hack you can use to get the number of UTF-8 bytes in a string:

                  function getUTF8ByteLength(s) {
                      return unescape(encodeURIComponent(s)).length;
                  }
                  

                  雖然在數據庫中存儲原生 Unicode 字符串可能會更好,這樣長度限制指的是實際字符,而不是某些編碼中的字節.

                  although probably it would be better to store native Unicode strings in the database so that the length limit refers to actual characters and not bytes in some encoding.

                  這篇關于如何在Javascript中獲取日文字符的長度?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發技術分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)
                  Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)
                  <legend id='ndRTq'><style id='ndRTq'><dir id='ndRTq'><q id='ndRTq'></q></dir></style></legend>
                3. <tfoot id='ndRTq'></tfoot>
                          <bdo id='ndRTq'></bdo><ul id='ndRTq'></ul>

                            <i id='ndRTq'><tr id='ndRTq'><dt id='ndRTq'><q id='ndRTq'><span id='ndRTq'><b id='ndRTq'><form id='ndRTq'><ins id='ndRTq'></ins><ul id='ndRTq'></ul><sub id='ndRTq'></sub></form><legend id='ndRTq'></legend><bdo id='ndRTq'><pre id='ndRTq'><center id='ndRTq'></center></pre></bdo></b><th id='ndRTq'></th></span></q></dt></tr></i><div class="oll50me" id='ndRTq'><tfoot id='ndRTq'></tfoot><dl id='ndRTq'><fieldset id='ndRTq'></fieldset></dl></div>

                            <small id='ndRTq'></small><noframes id='ndRTq'>

                              <tbody id='ndRTq'></tbody>
                          • 主站蜘蛛池模板: 涂层测厚仪_光泽度仪_uv能量计_紫外辐照计_太阳膜测试仪_透光率仪-林上科技 | 岛津二手液相色谱仪,岛津10A液相,安捷伦二手液相,安捷伦1100液相-杭州森尼欧科学仪器有限公司 | 陕西鹏展科技有限公司 | cnc精密加工_数控机械加工_非标平键定制生产厂家_扬州沃佳机械有限公司 | 煤矿人员精确定位系统_矿用无线通信系统_煤矿广播系统 | 专业的压球机生产线及解决方案厂家-河南腾达机械厂 | 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 合肥汽车充电桩_安徽充电桩_电动交流充电桩厂家_安徽科帝新能源科技有限公司 | 合肥触摸一体机_触摸查询机厂家_合肥拼接屏-安徽迅博智能科技 | 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科| cnc精密加工_数控机械加工_非标平键定制生产厂家_扬州沃佳机械有限公司 | 水质监测站_水质在线分析仪_水质自动监测系统_多参数水质在线监测仪_水质传感器-山东万象环境科技有限公司 | 论文查重_免费论文查重_知网学术不端论文查重检测系统入口_论文查重软件 | 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 - 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 | 亮化工程,亮化设计,城市亮化工程,亮化资质合作,长沙亮化照明,杰奥思【官网】 | 节流截止放空阀-不锈钢阀门-气动|电动截止阀-鸿华阀门有限公司 | SMN-1/SMN-A ABB抽屉开关柜触头夹紧力检测仪-SMN-B/SMN-C-上海徐吉 | 污水提升器,污水提升泵,污水提升装置-德国泽德(zehnder)水泵系统有限公司 | 数码管_LED贴片灯_LED数码管厂家-无锡市冠卓电子科技有限公司 | 湖南长沙商标注册专利申请,长沙公司注册代理记账首选美创! | 大型冰雕-景区冰雕展制作公司,3D创意设计源头厂家-[赛北冰雕] | 通辽信息港 - 免费发布房产、招聘、求职、二手、商铺等信息 www.tlxxg.net | 氧化锆陶瓷_氧化锆陶瓷加工_氧化锆陶瓷生产厂家-康柏工业陶瓷有限公司 | 石英陶瓷,石英坩埚,二氧化硅陶瓷-淄博百特高新材料有限公司 | 郑州水质检测中心_井水检测_河南废气检测_河南中环嘉创检测 | 药品仓库用除湿机-变电站用防爆空调-油漆房用防爆空调-杭州特奥环保科技有限公司 | 仪器仪表网 - 永久免费的b2b电子商务平台 | 粒米特测控技术(上海)有限公司-测功机_减速机测试台_电机测试台 | 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 浙江自考_浙江自学考试网 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 上海心叶港澳台联考一对一培训_上海心叶港澳台联考,港澳台联考一对一升学指导 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 砂石生产线_石料生产线设备_制砂生产线设备价格_生产厂家-河南中誉鼎力智能装备有限公司 | 全自动实验室洗瓶机,移液管|培养皿|进样瓶清洗机,清洗剂-广州摩特伟希尔机械设备有限责任公司 | 【星耀裂变】_企微SCRM_任务宝_视频号分销裂变_企业微信裂变增长_私域流量_裂变营销 | 上海办公室设计_办公楼,写字楼装修_办公室装修公司-匠御设计 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | ptc_浴霸_大巴_干衣机_呼吸机_毛巾架_电动车加热器-上海帕克 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 |