(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));

Comments

  1. Pingback: 1boatswain