JS获取地址栏参数并解决中文乱码问题

获取地址栏参数

function getQueryString(name) {   
      var reg = new RegExp( " (^|&) " + name + " = ([^&]*)(&|$) " );   
      var r = window.location.search.substr( 1 ).match( reg );   
      if( r != null ) return unescape( r[2] ); return null;   

}

解决中文乱码问题

浏览器默认使用的是 encodeURL 对汉字进行编码的,所以在解码的时候就需要使用 decodeURL 而不是上诉代码中的 unescape,只要将上诉代码中 unescape 修改为 decodeURL 就可以解决中文乱码问题了

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;
}

你可能感兴趣的:(JS获取地址栏参数并解决中文乱码问题)