sajid
sajid

Reputation: 257

Error: $.cookie is not a function

I have added the jQuery plugin with this code:

<script type="text/javascript" src="js/jquery.cookie.js"></script>

My code for activating cookie is below:

$(document).ready(function() {
    $('ul.sub_menu a').click(function() {
        $('#sliderid, .prodcls').css("display","none");
        $.cookie('links','linkdisplay',{ expires: 2 });

        // var txt = $(this).text();
        //console.log("you clicked"+txt);
     });

    var links = $.cookie('links');
    if (links == 'linkdisplay') {$('#sliderid, .prodcls').css("display","none");};
}); 

But in the firebug console its giving an error:

$.cookie is not a function [Break On This Error]var links = $.cookie('links');

What's wrong?

Upvotes: 2

Views: 7253

Answers (4)

jazkat
jazkat

Reputation: 5788

I've been trying to get the cookie with code:

jQuery.cookie('my_cookie')

which returned error, so I changed it to this to check it:

$.cookie('cc_cookie_accept')

which is now working. I'm assuming the version of the plugin is not compatible with jQuery version, like user529649 said.

Upvotes: 0

user529649
user529649

Reputation:

The Error means that the plugin is missing.

There could be lots of reasons for this, a few common ones are:

  • Jquery is loaded after the plugin, (that is; plugin before Jquery.)
  • The Plugin Path is wrong or it's not accessible by web server.
  • The Plugin is not compatible with JQuery version.

I hope this helps.

Upvotes: 2

dku.rajkumar
dku.rajkumar

Reputation: 18568

I am not sure if this is the exact problem but surely this is one of the problem.

$(document).ready(function() {

   // click Method start here
   $('ul.sub_menu a').click(function() {
       $('#sliderid, .prodcls').css("display","none");
       $.cookie('links','linkdisplay',{ expires: 2 });
       // var txt = $(this).text();
       //console.log("you clicked"+txt);
   });
   // click method ends here

 // this part is not inside any function, this will be executed on document ready before any click happens
 var links = $.cookie('links');
 if (links == 'linkdisplay') {$('#sliderid, .prodcls').css("display","none");};
});

The problem is, you are saving the cookies on click handler function but retrieving on document ready that is before any click happen to the element so there wont be any cookie with name links on document ready while you are trying to retrieve. Please see my comment in code

Upvotes: 2

Umesh Patil
Umesh Patil

Reputation: 10685

If jquery.cookie.js is not present in js folder, add it there by downloading from here.

Code seems fine. The line $.cookie('links','linkdisplay',{ expires: 2 }); saves the cookie with name 'links' and 'linkdisplay' as value.

You can refere Get all cookies with Javascrip for more information about coookie plugin.

If it didn't solve, please, update with your HTML code and let me know what exactly you want to save in cookies.

Upvotes: 1

Related Questions