開發中發現頁面傳中文字符會發生亂碼,有效的方法就是進行編碼傳值,接收后解碼。
傳值頁面:
var name = "測試";
window.open("index1.aspx?name=" + encodeURI(encodeURI(name)));
接收頁面:
var name = decodeURI(getUrlParam("name"));
function getUrlParam(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()進行解碼。對中文需要進行兩次編碼才能解決亂碼問題。