Solve the parameters passed location.href the address bar, and to solve the garbage problem parameters Chinese

Solve the parameters passed location.href the address bar, and to solve the garbage problem parameters Chinese

location.href="http://localhost:3000/m/search.html?keywords="+"你好";
//获取参数值“你好”
function getParamsByUrl(current, name) {
    //根据关键字获取搜索结果
    var index = current.indexOf('?');
    var str = current.substr(index + 1);
    var arr = str.split('&');
    // console.log(arr);
    var keuval;
    $.each(arr, function(index, value) {
        var arr2 = value.split('=');
        // console.log(arr2);
        if (arr2[0] == name) {
            keuval = arr2[1];
            console.log(keuval);
            return false;
        }
    })
    return keuval;
}
 keywords = getParamsByUrl(location.href, "keywords");//此时获取到的是中文乱码%E4%BD%A0%E5%A5%BD
 keywords = decodeURIComponent(keywords);//对中文字符进行解码,获取到中文字符
 console.log(keywords);//"你好"


Guess you like

Origin blog.csdn.net/abcdef12030/article/details/92576307