2011-07-07 43 views
2

頁發送如下:如何解決這個字符編碼問題?

<!DOCTYPE HTML> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<script> 
function send() { 
    var chinese = document.getElementById('chinese').value; 
    window.location = 'receive.html?' + chinese; 
} 
</script> 
</head> 
<body> 
    <input id="chinese" type="text" name="chinese" value="" /> 
    <button onclick="send()">Submit</button> 
</body> 
</html> 

接收頁面如下:

<!DOCTYPE HTML> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<script> 
window.onload = function() { 
    var chinese = location.search; 
    document.getElementById('chinese').value = chinese; 
} 
</script> 
</head> 
<body> 
    <input id="chinese" type="text" name="chinese" value="" /> 
</body> 
</html> 

的問題是,接收頁面不接收/正確顯示中國漢字。例如,它將顯示?%E9%A6%96%E9%A1%B5,而不是首頁,雖然它在瀏覽器地址欄的查詢字符串中顯示得很好。

回答

1

的價值,你應該形成URL之前URI編碼:

window.location = 'receive.html?' + encodeURIComponent(chinese); 

現在可能不是你所要做的,因爲它有可能是服務器可能會導致問題的所有,但你無論如何,通常都希望使用直接從文本字段提供的輸入進行編碼。