Skip to content

关于url中文乱码两次转码问题

跳转界面传参时传中文会出现乱码,在传递参数界面转码两次,如:

'../a.html?name='+encodeURIComponent(encodeURIComponent(name))'

在接收参数的界面解两次码,如:

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