2011-07-07 41 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); 

现在可能不是你所要做的,因为它有可能是服务器可能会导致问题的所有,但你无论如何,通常都希望使用直接从文本字段提供的输入进行编码。