本文介绍了有人可以向我解释一下 javascript getCookie() 中 while 循环的功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
抱歉问了一个问题...下面是W3C School的getCookie代码教程
sorry for a question to ask...Below is the tutorial of getCookie code from W3C School
谁能教我while (c.charAt(0)==' ') c = c.substring(1);
的作用是什么,而且既然是while循环,为什么它不会一直重复并卡在那里吗?
Would someone teach me what is the function of while (c.charAt(0)==' ') c = c.substring(1);
, and since it is while loop, why won't it keep repeating and stuck there?
谢谢...
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.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 "";
}
}
推荐答案
删除c
前面的空格.
既然是while循环,为什么不一直重复卡在那里呢?
while
循环只重复 while 值为真.字符串不能由无限空格组成.
while
loops only repeat while the value is true. You can't have a string made up of infinite spaces.
这篇关于有人可以向我解释一下 javascript getCookie() 中 while 循环的功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!