1. 2016
    Apr
    5

    Quick implementation to support JQuery Cookie

    Posted By


    jquery cookie

    JQuery Cookie is not longer being supported and is now superseded by JS Cookie. This sucks for developers that use jQuery Cookie plugin as a dependency on their applications.

    JQuery Cookie as been a main stay in a lot of applications since HTML4/XHTML and is used by default to handle cookies.

    The superseded version JS Cookie reverted back to native JavaScript Object Orientated Programming (OOP) to implement its functionalities, so including it as a library to your application would break the older JQuery Cookie version.

    Here is a simple implementation that will allow you to continue using your jQuery Cookie plug-in without having to change any code and still maintain support.

    First download the superseded version JS Cookie here then add it as an external library, then replace jquery.cookie.js with this implementation.

    
    
    (function ($, root, undefined) {
    
    	$.cookie = function() {
    		switch(arguments.length) {
    			case 1:
    				return Cookies.get(arguments[0]);
    			case 2:
    				return Cookies.set(arguments[0], arguments[1]);
    			case 3:
    				return Cookies.set(arguments[0], arguments[1], arguments[2]);
    		}
    	};
    	
    	$.removeCookie = function() {
    		switch(arguments.length) {
    			case 1:
    				return Cookies.remove(arguments[0]);
    			case 2:
    				return Cookies.remove(arguments[0], arguments[1]);
    		}
    	};
    
    })(jQuery, this);
    
    

    If you would like to make your jQuery Cookie more HTML5 friendly, you can add these functionalities to below what you have above.

    
    
    	$.persistence = function() {
    		if(typeof(Storage) !== undefined) {
    			switch(arguments.length) {
    				case 1:
    					return localStorage.getItem(arguments[0]);
    				case 2:
    				case 3:
    					return localStorage.setItem(arguments[0], arguments[1]);
    			}
    		} else {
    			switch(arguments.length) {
    				case 1:
    					return $.cookie(arguments[0]);
    				case 2:
    					return $.cookie(arguments[0], arguments[1]);
    				case 3:
    					return $.cookie(arguments[0], arguments[1], arguments[2]);
    			}
    		}
    	};
    	
    	$.removePersistence = function() {
    		if(typeof(Storage) !== undefined) {
    			switch(arguments.length) {
    				case 1:
    				case 2:
    					return localStorage.removeItem(arguments[0]);
    			}
    		} else {
    			switch(arguments.length) {
    				case 1:
    					return $.removeCookie(arguments[0]);
    				case 2:
    					return $.removeCookie(arguments[0], arguments[1]);
    			}
    		}
    	};
    
    

  2. About Emeka Echeruo

    Emeka Echeruo

    I love sports, football which I refuse to call soccer, and the outdoor especially walks in park. Software development is my passion, there is a beauty in creating something out of nothing but algebra that ends up becomes a part of a persons daily life. I love kids, dogs, nightlife and art because it finds you and moves you emotionally!

  3. Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.