I'm trying to get some data from a cookie. I set the cookie with CodeIgniter and here is my code
$c = array('name' => 'total', 'value' => $total, 'path' => '/');
$this->input->set_cookie($c);
Then, I want to take the data with javascript. This is what i've tried
function getCookie(c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0; i<ARRcookies.length; i++) {
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name) {
return unescape(y);
}
}
}
console.log(getCookie('total'))
I got the function from here but it doesn't work to my code. Is there anyway to achieve this ?