当前位置:网站首页>Solve the Chinese garbled code of URL in JS - decoding

Solve the Chinese garbled code of URL in JS - decoding

2022-04-23 20:24:00 First code

solve url The statement , There are three main ways :unescape,encodeURI as well as encodeURIComponent(), about url It contains Chinese , It was found that the previously used did not work , Changed to decodeURL Chinese decoding is normal , The code is as follows

var url = window.location.search; // obtain url in "?" The string after the sign    
                    var theRequest = new Object();
                    if (url.indexOf("?") != -1) {
                        var str = url.substr(1);
                        strs = str.split("&");
                        for (var i = 0; i < strs.length; i++) {
                            // That's the problem with this sentence 
                           console.log( theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]));
                            // I used it before unescape()
                            // There will be garbled code   
                        }
                    }

Code encapsulation reference

 function getUrlParam(name) {

      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // Construct a regular expression object with target parameters 
        var r = window.location.search.substr(1).match(reg); // Match target parameters 
        if (r != null) return decodeURI(r[2]); 
        return null; // Return parameter value 
}
getUrlParam( Parameters 1);
getUrlParam( Parameters 2);

 

版权声明
本文为[First code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210551267745.html