Reputation: 257
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
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
Reputation:
There could be lots of reasons for this, a few common ones are:
I hope this helps.
Upvotes: 2
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
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