<menuitem id="tjl3v"></menuitem>
<var id="tjl3v"></var>
<var id="tjl3v"></var><var id="tjl3v"></var><menuitem id="tjl3v"><dl id="tjl3v"><progress id="tjl3v"></progress></dl></menuitem>
<cite id="tjl3v"><video id="tjl3v"><listing id="tjl3v"></listing></video></cite>
<var id="tjl3v"><strike id="tjl3v"><listing id="tjl3v"></listing></strike></var>
<menuitem id="tjl3v"><dl id="tjl3v"><progress id="tjl3v"></progress></dl></menuitem>
<cite id="tjl3v"></cite>
<var id="tjl3v"></var><var id="tjl3v"></var>
<var id="tjl3v"><strike id="tjl3v"></strike></var>
<var id="tjl3v"><dl id="tjl3v"><listing id="tjl3v"></listing></dl></var>
<cite id="tjl3v"><video id="tjl3v"><listing id="tjl3v"></listing></video></cite><var id="tjl3v"></var>

js通過window.location.href=url頁面跳轉中文參數亂碼問題解決

2016-01-27 19:14:00
張精制
原創 4001
摘要: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;
}

發表評論
評論通過審核后顯示。
怎么买彩票