Skip to content

Instantly share code, notes, and snippets.

@HQMIS
Created August 20, 2013 09:16
Show Gist options
  • Save HQMIS/6279161 to your computer and use it in GitHub Desktop.
Save HQMIS/6279161 to your computer and use it in GitHub Desktop.
中文Cookie问题
window.onload = function (){
var arrStr = document.cookie.split(";");
for(var i=0; i<arrStr.length; i++){
var temp = arrStr[i].split("=");
if(temp[0].trim()=="username"){
var usr = temp[1].trim();
alert(usr);
alert(typeof(usr));
var test = "\345\220\257\346\225\217";
alert(test);
alert(typeof(test));
alert(usr == test);
document.getElementById("user").innerHTML = decodeURIComponent(escape(usr))+'<strong class="caret"></strong>';
document.getElementById("login").style.display = "none";
}
}
}
上述函数放在index.js中,index.js是通过<script src="/static/js/index.js" charset="UTF-8"></script>引入的
同时也在Vim下通过:set fileencodeing查看了index.js和*.html的编码,都是utf8
使用decodeURIComponent(escape(usr)),未能正常显示汉字
而使用decodeURIComponent(escape(test)), 则正常显示出来汉字
alert(usr); 显示 "\345\220\257\346\225\217"
而alert(test); 显示乱码
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment