How to fix this character encoding issue?
The send page is as follows:
<!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 开发者_Python百科onclick="send()">Submit</button>
</body>
</html>
The receive page is as follows:
<!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>
The problem is that the receive page does not receive/display the Chinese characters correctly. For example, instead of 首页
it displays ?%E9%A6%96%E9%A1%B5
, though it does display well in the query string in the browser address bar.
The value should be URI-encoded before you form the URL:
window.location = 'receive.html?' + encodeURIComponent(chinese);
Now that may not be all you have to do, because it's possible that the server could be causing problems, but you generally want to do that encoding anyway with input provided directly from an text field.
精彩评论