请问我如何在文本中获取用户名... 我在javascript上使用cookie,但我尝试了一下,但没有成功
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
var user=getCookie("username");
alert(user);
我用它来设置cookie:
function setCookie(cname,cvalue) {
document.cookie = cname + "=" + cvalue;
}
请问我该如何实现?