一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码:
1.传参页面
Javascript代码:
function send(){
var url = "test01.html";
var userName = $("#userName").html();
window.open(encodeURI(url + "?userName=" + userName));
}
2. 接收参数页面:test02.html
var urlinfo = window.location.href;//获取url
var userName =getQueryString('userName')
$(“#userName”).html(userName );
在网上找到下面getQueryString方法,但是url中存在中文参数的时候,出现乱码。
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;
}
encodeURI() 函数可把字符串作为 URI 进行编码,而decodeURI函数对URI进行解码.unescape() 函数可对通过 escape() 编码的字符串进行解码。游览器默认使用的encodeURI进行编码,所有在解码的时候应用使用decodeURI。
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURI(r[2]); return null;
}