2

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?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
sajid
  • 247
  • 7
  • 15
  • 1
    Have you added the jquery-cookie before jquery ? (I hope not.) –  Jan 11 '12 at 07:46
  • It could be what Omeid wrote, or: Does the new javascript jquery.cookie.js is loaded properly? Is the path to it set up with all slashes needed? – koressak Jan 11 '12 at 07:53
  • i done a mistake i added it before jquery library and now i added it after query library problem solved – sajid Jan 11 '12 at 08:44
  • 1
    @OmeidHerat Since your comment solved the problem you should probably post it as an answer so sajid can mark it as solved. – Filip Jan 11 '12 at 08:58
  • @omeid post it as answer so people could get help i will mark it – sajid Jan 11 '12 at 09:01
  • @sajid Good, I will post it in a second. I am glad that it helped. –  Jan 11 '12 at 09:08
  • @Rory McCrossan great editing – sajid Jan 11 '12 at 09:08

4 Answers4

2

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

dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
  • You're totally right. But depending on what @sajid wants to do, this is not necessarily wrong. – Filip Jan 11 '12 at 08:36
2

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.

1

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.

Umesh Patil
  • 10,475
  • 16
  • 52
  • 80
0

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.

jazkat
  • 5,600
  • 3
  • 25
  • 17