$.cookie is not a function

No but I can show you how to make one very easy like ...

Create a seperate js file, name it what you want, for ease of this, i'll call it jCook.

In your header, after you add jQuery, add your new file:

<script type="text/javascript" src="jquery.js"></script>  
<script type="text/javascript" src="jCook.js"></script>  <!-- here it is! -->

And below is the EASY code to put in the file:

(function($) {
    if (!$.setCookie) {
        $.extend({
            setCookie: function(c_name, value, exdays) {
                try {
                    if (!c_name) return false;
                    var exdate = new Date();
                    exdate.setDate(exdate.getDate() + exdays);
                    var c_value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
                    document.cookie = c_name + "=" + c_value;
                }
                catch(err) {
                    return false;
                };
                return true;
            }
        });
    };
    if (!$.getCookie) {
        $.extend({
            getCookie: function(c_name) {
                try {
                    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);
                    };
                }
                catch(err) {
                    return false;
                };
                return false;
            }
        });
    };
})(jQuery);

The try catch and what not just helps to ensure you get a "false" return if you do somthing wrong, but really shouldn't ever be an issue. To use the code is easy...

On your page, where your load code or whatever is just do the following:

$.setCookie("nameOfCookie", "someValue", 30); // where 30 is the number 
// of days to expire, or you could leave blank as:
$.setCookie("nameOfCookie", "someValue")

// And to retrieve your cookie
$.getCookie("nameOfCookie");

See how simple that was!?

And just to resolve, below is a real world example use to save a state of a select drop down menu

$(function(){
        $("select[name=somthing]").change(function(e) {
            $.setCookie("selectThis", $(this).val());
        });
        //  And to use ...
        if ($.getCookie("selectThis")) $("select[name=somthing]").val( $.getCookie("selectThis") ).change();
});

That means that the $.cookie plugin isn't being included in the page, at least not being it's getting called. Make sure it's both being included, and is being included before it's getting used. Include it just after jQuery itself to be safe.

Just a tip: Several other plugins rely on the cookie plugin (but don't necessarily check if it exists before calling it), you could be using one.


Do you have the jQuery cookie plugin?