Home >Web Front-end >JS Tutorial >What to do if jquery url is Chinese garbled

What to do if jquery url is Chinese garbled

藏色散人
藏色散人Original
2021-01-28 10:33:452309browse

The solution to the Chinese garbled jquery url: first get the parameters in the url; then construct a regular expression object containing the target parameters; then match the target parameters; and finally return the parameter value.

What to do if jquery url is Chinese garbled

The operating environment of this tutorial: windows7 system, jquery1.2.6 version, DELL G3 computer.

Recommended: jquery video tutorial

Jquery gets the parameters in the URL and solves the problem of Chinese garbled parameters.

Method 1:

//获取url中的参数
function getUrlParamCN(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = decodeURI(window.location.search).substr(1).match(reg); //匹配目标参数
if (r != null) return unescape(r[2]); return null; //返回参数值
}

Method 2:

1. Pass parameter page.html

<script type="text/javascript">
    var name = &#39;abc&#39;;  //字符串英文
    var num = 998;   //数字
    var cla= &#39;电脑&#39;;  //中文
    var _t = encodeURI(encodeURI(cla));  
    //中文需要进行两次encodeURI转码( encodeURI:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。)
    window.location.href = "../aa.html?name=" + name(字符串) + "&num=num (数字)" + "&cla=_t (中文)";
 
</script>

2. Receive parameter page.html

(Final page address: ../aa.html?name=abc&num=998&cla=%E7%94%B5%E8%84%91)

<script type="text/javascript">
    //获取地址参数
    var _name = getQueryString("name");
    var _num=getQueryString("num");
    var _t = getQueryString("cla"); //此为中文参数,需转码
    var title = decodeURI(_t); //只需要转一次码  
 
    console.log(title);//此处打印出来是中文。
 
 
 
    function getQueryString(name) {  
        //正则表达式,获取地址中的参数
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");  
        var r = window.location.search.substr(1).match(reg);  
 
        if (r != null) return unescape(r[2]); return null; 
 
    }
 
</script>

The above is the detailed content of What to do if jquery url is Chinese garbled. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn