document.cookie = "happy=random stuff";
function readCookie(name) {
var cookie,cname,i;
cookie = document.cookie.split(";");
cname = name;
for (i=0; i<=cookie.length; i++) {
var c = cookie[i];
if (c.indexOf(cname) == 0) {
alert(c.substring(cname.length,c.length));
return c.substring(cname.length,c.length);
}
}
return null;
}
readCookie("happy");
http://jsfiddle.net/a2R3j/
It appears that this line is wrong:
if (c.indexOf(cname) == 0) {
it works perfectly. However jsfiddle says there is an error. Is there actually anything wrong?
EDIT FOR CLARITY: I want it to return the cookie’s content by calling the function getCookie(name here)
another edit for clarity:
Yusaf Code: 
Code with indexOf: 
http://jsfiddle.net/6qLZe/
use
.split("=")[1]rather than your substring method