在使用Microsoft edge的时候,运行以下代码会报兼容性错误,但是一直没解决
'content-type' header charset value should be 'utf-8'.
使用chrome不会存储cookie 也没有报错
使用火狐可以成功存储cookie
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script >
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(";");
for(var i=0;i<ca.length;i++) {
var c = ca[i].trim();
if(c.indexOf(name)==0) {
return c.substring(name.length,c.length);
}
}
return "";
}
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime()+exdays*24*60*60*1000);
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires;
}
function checkCookie() {
var user = getCookie("username");
// delCookie("username");
if(user != "") {
alert("welcome, " + user + "!");
}
else {
user = prompt("please input your name: ","");
if(user!=""&&user!=null) {
setCookie("username",user,60);
}
}
}
//删除cookie
function delCookie(name){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
</script>
</head>
<body onl oad="checkCookie()">
</body>
</html>
标签:存储,JavaScrip,学习,cname,cookie,var
From: https://www.cnblogs.com/ayubene/p/17831306.html