猿问

var end = start + document.cookie.indexOf(";",start); 这一行代码 为什找不到 返回-1

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

</head>

<script type="text/javascript">

//检测cookie

function getcookie(myname) {

if (document.cookie.length > 0) {

var start = document.cookie.indexOf(myname + "=");

if (start != -1) {

start = start + myname.length + 1;

var end =document.cookie.indexOf(";", start);

if (end == -1) {

end = document.cookie.length;

}

return unescape(document.cookie.substring(start, end));

}

}

return ""

}

//创建cookie

function setcookie(myname, value, expireday) {

var exdate = new Date();

exdate.setDate(exdate.getDate() + expireday);

document.cookie = myname + "=" + escape(value) + ((expireday == null) ? "" : ";expires=" + exdate.toGMTString())

}

function checkcookie() {

username = getcookie('username')

if (username != null && username != "") {

alert('Welcome again ' + username + '!')

} else {

username = prompt('Please enter your name:', "")

if (username != null && username != "") {

setcookie('username', username, 365)

}

}

}

</script>

<body onload="checkcookie()"></body>

</html>


zhaoqihao
浏览 1483回答 1
1回答

柠檬酸钠

也就是说从start开始只剩下一个或者没有cookie了,所以找不到分号,返回-1了
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答