function getQueryString(key){
var reg = new RegExp("(^|&)"+key+"=([^&]*)(&|$)");
var result = window.location.search.substr(1).match(reg);
return result?decodeURIComponent(result[2]):null;
}
//使用方式
var pstr = encodeURIComponent(JSON.stringify(PARAMS));
var URI = getQueryString('params');
// 如果URI 中包含了"%" 转化为"%25"
if(URI.indexOf('%') > -1) {
URI = URI.replace(/%/g,'%25')
}
//编码和解码方式
1. encodeURI() 和 decodeURI()
2. encodeURIComponent() 和 decodeURIComponent()
var str = "http://www.baidu.com?params=张三";
console.log(encodeURI(str));
//http://www.baidu.com?params=%E5%BC%A0%E4%B8%89
var strDecode = encodeURI(str);
console.log(decodeURI(strDecode));
console.log(encodeURIComponent(str));
//http%3A%2F%2Fwww.baidu.com%3Fparams%3D%E5%BC%A0%E4%B8%89
var strdecodeURI = encodeURIComponent(str);
console.log(decodeURIComponent(strdecodeURI));
获取url参数 对中文参数进行编码和解码
最新推荐文章于 2024-07-30 12:22:43 发布