30 December 2010

getCookie() example from w3schools defines extra global variables

I've found following peace of code in our code base and it made me not happy:
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}

What's wrong? This function defines 2 global variables: c_start and c_end.

Author should learn how to use var keyword in JavaScript. But 10 minutes later I found following page and realized that it's just copy-paste from very popular website:
http://www.w3schools.com/JS/js_cookies.asp

Really not good. W3schools have to fix it. People doesn't learn JS, they just do copy-paste.

1 comments:

Profile

My Photo
Kyiv, Ukraine
Dev Team Lead, Scrum Master