Zig*_*rth 4 javascript unicode cookies utf-8
任何人都可以帮助我理解如何存储另一种语言的cookie值,以及如何以该语言再次检索它.
在存储后检索时,我的外语cookie似乎变成了垃圾.
一些代码:
写cookie代码:
function writecook() {
document.cookie = "lboxcook=" + document.getElementsByTagName('input')[0].value;
//input[0] is the input box who's value is stored
}
Run Code Online (Sandbox Code Playgroud)
检索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>
Run Code Online (Sandbox Code Playgroud)
谢谢.
Tim*_*own 17
使用encodeURIComponent()设置cookie并在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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6619 次 |
| 最近记录: |