I created a jQuery plugin to do various things with cookies. The trouble I am having is the function I created to read the cookie and return the value. No matter what I do, when I call the function as so from the main page
test = $.cookie({
action:'get',
cookie_name:LOGIN_COOKIE_NAME
});
the return is ALWAYS undefined. Console output is as follows:
Value found is => 10
Returned => undefined
I'm at my wit's end trying to figure out what's going on here. I've tried everything I know to do and spent literally hours Googling this with no solution. Below is the code in my jQuery plugin:
// Get the value of the sought after cookie
function get_cookie(){ var value = ''; // Get the name to search for var name = settings.cookie_name; // Divide the cookie string into an array var ca = document.cookie.split(';'); // Loop through each element in the main array we just created match_found = false; for(var i=0; i<ca.length; i++){ // Clear the whitespace from the ends to prevent mismatches and split each > element into the cookie name and value var temp = ca[i].trim().split('='); // Now check to see if this is the cookie we are looking for if(temp[0] == name && !match_found){ match_found = true; // If it is, then return the cookie's value cookie_value = temp[1]+''; if(settings.action != 'check'){ value = cookie_value; }else{ value = true; } } } if(!match_found && settings.action == 'check'){ return false; }else if(!match_found && settings.action != 'check'){ return 'dooby'; }else{ console.log('Value found is => '+value); return value; } }
Does anyone have any insight into what's causing this and how it can be corrected?
Here is the entire code of my jQuery plugin
(function($){
$.cookie = function(options) {
// Default Settings
var settings = $.extend({
action : 'check', // create or delete
cookie_name : 'cookie',
cookie_value : 'default',
expiration : 'Sun, 31 Dec 2017 12:00:00 GMT',
path : '/'
}, options);
switch(settings.action){
case "create":
create_cookie();
break;
case "delete":
delete_cookie();
break;
case "check":
return check_for_cookie();
break;
case "get":
get_cookie();
break;
default:
get_cookie();
break;
}
// Create the cookie
function create_cookie(){
try{
document.cookie = settings.cookie_name+"="+settings.cookie_value+"; expires="+settings.expiration+"; path="+settings.path;
console.log("Cookie Created");
}catch(e){
console.log(e);
}
}
// Delete said cookie
function delete_cookie(){
document.cookie = settings.cookie_name+"="+settings.cookie_value+"; expires=Sun, 29 Dec 2013 12:00:00 GMT; path="+settings.path;
}
// Get the value of the sought after cookie
function get_cookie(){
var value = '';
// Get the name to search for
var name = settings.cookie_name;
// Divide the cookie string into an array
var ca = document.cookie.split(';');
// Loop through each element in the main array we just created
match_found = false;
for(var i=0; i<ca.length; i++){
// Clear the whitespace from the ends to prevent mismatches and split each element into the cookie name and value
var temp = ca[i].trim().split('=');
// Now check to see if this is the cookie we are looking for
if(temp[0] == name && !match_found){
match_found = true;
// If it is, then return the cookie's value
cookie_value = temp[1]+'';
if(settings.action != 'check'){
value = cookie_value;
}else{
value = true;
}
}
}
if(!match_found && settings.action == 'check'){
return false;
}else if(!match_found && settings.action != 'check'){
return 'dooby';
}else{
console.log('Value found is => '+value);
return value;
}
}
// Check to see if said cookie exists
function check_for_cookie(){
var is_set = get_cookie(settings.cookie_name);
if(is_set){
return true;
}else{
return false;
}
}
};
}(jQuery));
SOLUTION:
Thanks to OJay below for the keen eye and the solution to this. Scroll down and give him some upvotes.
The call to get_cookie() in the main switch statement that handles what action to take was not returning anything. So following OJay 's solution it now works by looking like this:
case "get":
return get_cookie();
break;