$ .cookie is not a function

When I try to load my page using jquery when clicking the following line:

if ($.cookie('compare') != null) 

I get the error $ .cookie is not a function. Has anyone seen this before?

+29
javascript jquery
Oct 27 2018-10-27
source share
10 answers

This means that the $.cookie not included in the page, at least not by calling it. Make sure it is turned on and on before it is used. Turn it on immediately after jQuery is safe.

Just a hint: several other plugins rely on the cookie plugin (but do not necessarily check if this exists before calling it), you can use it.

+52
Oct 27 2018-10-27
source share

Do you have a jQuery cookie ?

+10
Oct 27 2018-10-27
source share

No, but I can show you how to do it very easily ...

Create a separate js file, name it whatever you want, for the convenience of this I will name it jCook.

In your header after adding jQuery add a 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 place 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); 

Try to catch and that not only helps to guarantee you receive a β€œfalse” return if you have something wrong, but in fact it should never be a problem. Using code is easy ...

On your page, where is your download code or something else, follow these steps:

 $.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 it was !?

And just to solve, the following is an example of a real world to save the state of the selected 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(); }); 
+4
Feb 23 '12 at 19:28
source share

In principle, there can be various reasons for this problem.

  • You may not have added the jquery plugin.
  • Be sure to include the jQuery file before the cookie plugin
  • If you have already done the above two then try downloading the new version of the jquery cookie plugin . which also solved my problem.
+3
Dec 04 '15 at 11:40
source share

Try to enable Mod_Sec for your domain or ask your hosting company to enable it. Hope this helps.

0
Apr 03 2018-12-12T00:
source share

The same problem arose in our installation of Drupal 7. It was caused, if I understand correctly, by setting ModSecurity in cPanel, which blocks the file. See http://forums.cpanel.net/f5/mod-security-blocking-jquery-cookie-javascript-drupal-installation-191002.html

0
Jan 20 '13 at 14:28
source share

I use this code -

 if( $.cookie('siteLayout') != undefined ){ if( $.cookie('siteLayout') == 'compact' ) $(settings.bodyElem).addClass('container'); } 

And it works great. But I needed to use a different version of jQuery on a specific page where the version was 1.7.2, but I actually used 1.10.2. After using multiple versions of jQuery, I got this error -

TypeError: $ .cookie is not a function

then i used jQuery.cookie instead of $.cookie and it works. Hope this also helps;)

0
Dec 17 '13 at 8:26
source share

For me, the problem was fixed after downloading the latest version of jquery.cookie js

-one
Mar 27 '14 at 19:07
source share

$ .cookie is not a standard jQuery function. When you enable the cookie script, be sure to enable jQuery FIRST and then the cookie plugin. those.

 <script src="~/Scripts/jquery-2.0.3.js" type="text/javascript"></script> 

must always precede

 <script src="~/Scripts/jquery_cookie.js" type="text/javascript"></script> 
-one
Apr 05 '19 at 10:08
source share
 <script src="js/jquery.mobile-1.4.5.min.js"></script> <script src="js/jquery.cookie.js"></script> 

the first plugin should be juqery.js and the second plugin should be cookie.js

-2
Mar 15 '17 at 1:57
source share



All Articles