本文介绍了如何在cookie中存储其他语言(unicode)并重新获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以帮助我理解如何存储另一种语言的cookie值,而不是
如何以该语言重新获取它。
Can anyone help me understand how to store a cookie value that is in another language and than how to retrieve it again in that language.
在储存后,我的外语cookie似乎变成垃圾。
I seem to have my foreign language cookies turn to garbage when retrieved after being stored.
撰写Cookie代码:
Write cookie code:
function writecook() {
document.cookie = "lboxcook=" + document.getElementsByTagName('input')[0].value;
//input[0] is the input box who's value is stored
}
检索Cookie代码:
Retrieve Cookie code:
<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()
p>
Use encodeURIComponent()
when setting the cookie and decodeURIComponent()
when retrieving it.
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;
}
这篇关于如何在cookie中存储其他语言(unicode)并重新获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!