js通过window.location.href=url页面跳转中文参数乱码问题解决

2016-01-27 19:14:00
张精制
原创 2538
摘要:js通过window.location.href=url页面跳转中文参数乱码问题解决

在A页面中点击按钮跳转B页面,跳转代码如下:

var url = "addinfo.html?jsapi_ticket="+jsapi_ticket+"&address="+$('#address').html();

window.location.href=url;

上面的address参数是中文,此时在B页面可以有多种方式接收参数:

第一种:

                    var 
                    request = []; 
                    var 
                    jsapi_ticket; 
                    var 
                    signature; 
                    var 
                    searchStr = 
                    location.
                    search; 
                    request = 
                    searchStr.
                    substr(
                    1).
                    split(
                    "&"); 
                    for (
                    var 
                    i = 
                    0; 
                    i < 
                    request.
                    length; 
                    i++) { 
                    switch (
                    request[
                    i].
                    split(
                    "=")[
                    0]) { 
                    case 
                    "jsapi_ticket": 
                    jsapi_ticket = 
                    request[
                    i].
                    split(
                    "=")[
                    1]; 
                    break; 
                    case 
                    "chuang": 
                    wxnickname = 
                    request[
                    i].
                    split(
                    "=")[
                    1]; 
                    $(
                    "#createName").
                    val(
                    wxnickname); 
                    break; 
                    case 
                    "address": 
                    address = 
                    request[
                    i].
                    split(
                    "=")[
                    1]; 
                    $(
                    "#gravePlace").
                    val(
                    getCharFromUtf8(
                    address)); 
                    break; 
                    default: 
                    break;
    }
}

但是会出现中文乱码问题,我们需要以下两个方法对url的UTF-8编码转化为中文字符,调用方式getCharFromUtf8(url);

                      function 
                      getCharFromUtf8(str) { 
                      var 
                      cstr = 
                      ""; 
                      var 
                      nOffset = 
                      0; 
                      if (str == 
                      "") 
                      return 
                      "";
    str = str.
                      toLowerCase(); 
                      nOffset = str.
                      indexOf(
                      "%e"); 
                      if (
                      nOffset == -
                      1) 
                      return str; 
                      while (
                      nOffset != -
                      1) { 
                      cstr += str.
                      substr(
                      0, 
                      nOffset);
        str = str.
                      substr(
                      nOffset, str.
                      length - 
                      nOffset); 
                      if (str == 
                      "" || str.
                      length < 
                      9) 
                      return 
                      cstr; 
                      cstr += 
                      utf8ToChar(str.
                      substr(
                      0, 
                      9));
        str = str.
                      substr(
                      9, str.
                      length - 
                      9); 
                      nOffset = str.
                      indexOf(
                      "%e");
    } 
                      return 
                      cstr + str;
} 
                      //
                      将编码转换成字符 
                      function 
                      utf8ToChar(str) { 
                      var 
                      iCode, 
                      iCode1, 
                      iCode2; 
                      iCode = 
                      parseInt(
                      "0x" + str.
                      substr(
                      1, 
                      2)); 
                      iCode1 = 
                      parseInt(
                      "0x" + str.
                      substr(
                      4, 
                      2)); 
                      iCode2 = 
                      parseInt(
                      "0x" + str.
                      substr(
                      7, 
                      2)); 
                      return String.
                      fromCharCode(((
                      iCode & 
                      0x0F) << 
                      12) | ((
                      iCode1 & 
                      0x3F) << 
                      6) | (
                      iCode2 & 
                      0x3F));
}

第二种:(简单方便无乱码)

    
                    function 
                    GetQueryString(name)
                    //
                    获取
                    url
                    对应字段 { 
                    var 
                    reg = 
                    new RegExp(
                    "(^|&)"+ name +
                    "=([^&]*)(&|$)"); 
                    var 
                    r = 
                    window.
                    location.
                    search.
                    substr(
                    1).
                    match(
                    reg); 
                    if(
                    r!=
                    null)
                    return 
                    decodeURI(
                    r[
                    2]); 
                    return null;
}

发表评论
评论通过审核后显示。