任何人都可以帮助我了解如何存储另一种语言的 cookie 值,而不是 如何以该语言再次检索它。
我的外语 cookie 在存储后检索时似乎变成了垃圾。
部分代码:
编写cookie代码:
function writecook() {
document.cookie = "lboxcook=" + document.getElementsByTagName('input')[0].value;
//input[0] is the input box who's value is stored
}
检索 Cookie 代码:
<script language="JavaScript">
function get_cookie ( cookie_name )
{
var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
if ( results )
return ( unescape ( results[2] ) );
else
return null;
}
</script>
谢谢。
最佳答案
设置 cookie 时使用 encodeURIComponent()
,检索时使用 decodeURIComponent()
。
var cookieValue = document.getElementsByTagName('input')[0].value;
document.cookie = "lboxcook=" + encodeURIComponent(cookieValue);
function get_cookie(cookie_name) {
var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
return results ? decodeURIComponent(results[2]) : null;
}
关于javascript - 如何在 cookie 中存储其他语言 (unicode) 并再次取回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4901633/