简体   繁体   中英

Javascript: encodeURI() / encodeURIComponent() charset

Is there any way to specify charset in Javascript's encodeURI() or encodeURIComponent()? Eg:

encodeURIComponent("例子", "UTF-8") outputs %E4%BE%8B%E5%AD%90

encodeURIComponent("例子", "GBK") outputs %C0%FD%D7%D3

My solution is to used a npm package urlencode and browserify .

Write in urlencode.js:

var urlencode = require("urlencode");
module.exports = function (s) { return urlencode(s, "gbk"); }

browserify urlencode.js --s encode > bundle.js

And in bundle.js, a function called encode is declared.

Based on the earlier edit of this question before you revised it, it seems like you're trying to access various Baidu Baike based on different search terms you submit. My answer is not relevant to your new JS question, but the simple solution to your old problem (and would obviate the need for a JS solution) is to specify the character encoding in the Baidu Baike URL: &enc= . All the following resolve correctly:

const _encodeURIComponent = (x) =>
    x   
        .split('')
        .map((y) => {
            if (!RegExp(/^[\x00-\x7F]*$/).test(y)) {
                return iconv
                    .encode(y, encoding)
                    .reduce((a, b) => a + '%' + b.toString(16), '') 
                    .toUpperCase();
            } else {
                return y;
            }   
        })  
        .join('');

Replace encoding with desired encoding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM