Skip to content

Instantly share code, notes, and snippets.

@scriptnull
Last active March 19, 2017 00:33
Show Gist options
  • Save scriptnull/9804779 to your computer and use it in GitHub Desktop.
Save scriptnull/9804779 to your computer and use it in GitHub Desktop.
searching for the cookie value by giving cookie key value
//cookie Search if many cookies exist
function getMyCookie(key)
{
var cook = document.cookie.split(";");
var str ;
var vals = new Array();
for(var x = 0 ; x < cook.length ; x++ )
{
var temp = cook[x].split("=");
for(var y = 0 ; y < temp.length ; y++)
{
vals.push(temp[y]);
}
}
for(var x = 0 ; x < vals.length ; x++ )
{
if( key.toString() === vals[x] )
return vals[x+1];
}
}
var val = getMyCookie("rollno");
console.log(val);
//cookie search if only one cookie exists
var cook = document.cookie.split("=");
console.log(cook);
function mycookiesearch(str)
{
for(var x = 0 ; x < cook.length ; x++)
{
if(cook[x] == str )
{
return cook[x+1] ;
}
}
}
mycookiesearch("rollno");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment