Title: JQuery and cookie usage.
Last modified: August 30, 2016

---

# JQuery and cookie usage.

 *  [Mantasve11](https://wordpress.org/support/users/mantasve11/)
 * (@mantasve11)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/jquery-and-cookie-usage/)
 * Hi, I try to insert to “widgets” the following code:
 *     ```
       <link rel="stylesheet" type="text/css" href="/wp-content/jquery.fancybox.css">
       <style type="text/css">
        a {outline: 0 none; text-decoration: none;}
        a img {border: 0 none;}
        #wrap { width: 800px;  margin: 20px auto; font-family: arial, sans-serif; font-size: 14px;}
        p.counter span {font-weight: bold;}
       </style>
       <script src="/wp-content/jquery.js"></script>
       <script type="text/javascript" src="/wp-content/jquery.fancybox.js"></script>
       <script type="text/javascript" src="/wp-content/jquery.cookie-1.3.0.js"></script>
       <script>
       $(document).ready(function () {
           // create cookie
           var visited = $.cookie('visited'); // visited = 0
           if (visited == 1) {
               $("p.counter").html("Tuoj issoks langas");
               // open fancybox after 3 secs on 4th visit
               setTimeout(function () {
                   $.fancybox.open({
                       type: 'iframe',
                   href : 'http://labas.lt', 
   
                   });
               }, 3000);
           } else {
               visited++; // increase counter of visits
               $("p.counter span").append(visited);
               // set new cookie value to match visits
               $.cookie('visited', visited, {
                   expires: 3// expires after one day
               });
               return false;
           }
       }); // ready
       </script>
       ```
   
 * But it does not work for me.
 * Error: Uncaught TypeError: Cannot read property ‘cookie’ of undefined
 *     ```
       var visited = $.cookie('visited'); // visited = 0
       ```
   
 * How can I fix this error?
    If you insert a code into a separate file for example.
   file.php it works. But when you insert it into the wordpress is dont work.

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [11 years, 1 month ago](https://wordpress.org/support/topic/jquery-and-cookie-usage/#post-6314858)
 * Don’t do this:
 *     ```
       $(document).ready(function () {
       ```
   
 * Do this:
 *     ```
       jQuery(document).ready(function($) {
       ```
   
 * [https://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers](https://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers)
 *  Thread Starter [Mantasve11](https://wordpress.org/support/users/mantasve11/)
 * (@mantasve11)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/jquery-and-cookie-usage/#post-6314860)
 * Or else I get this error:
    Uncaught TypeError: undefined is not a function `$(
   document).ready(function () {`
 * If i use the WordPress jquery.js
 *  Thread Starter [Mantasve11](https://wordpress.org/support/users/mantasve11/)
 * (@mantasve11)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/jquery-and-cookie-usage/#post-6314862)
 * Thanks bro. Very fast support.
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [11 years, 1 month ago](https://wordpress.org/support/topic/jquery-and-cookie-usage/#post-6314863)
 * Please do continue to use WordPress jquery.js – That is the library that all 
   the plugins distributed on WordPress.org use and so changing it can result in
   breaking a lot of stuff!
 *  Thread Starter [Mantasve11](https://wordpress.org/support/users/mantasve11/)
 * (@mantasve11)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/jquery-and-cookie-usage/#post-6314887)
 * Another question. How to make a pop-up window will appear only once per day. 
   For now many times to refresh the page and display both.
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [11 years, 1 month ago](https://wordpress.org/support/topic/jquery-and-cookie-usage/#post-6314899)
 * Before this:
 *     ```
       var visited = $.cookie('visited'); // visited = 0
       ```
   
 * Check if the cookie does not exists…
 *     ```
       if ($.cookie('visited').length === 0) {
           var visited = $.cookie('visited'); // visited = 0
           ...
       }
       ```
   
 * Then replace this:
 *     ```
       var visited = $.cookie('visited'); // visited = 0
       ```
   
 * With this:
 *     ```
       var visited = $.cookie('visited', '0', {expires: 1 }); // visited = 0, expires in 1 day
       ```
   
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [11 years, 1 month ago](https://wordpress.org/support/topic/jquery-and-cookie-usage/#post-6314903)
 * [https://github.com/carhartl/jquery-cookie#readme](https://github.com/carhartl/jquery-cookie#readme)
 *  Thread Starter [Mantasve11](https://wordpress.org/support/users/mantasve11/)
 * (@mantasve11)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/jquery-and-cookie-usage/#post-6314924)
 *     ```
       } else {
               visited++; // increase counter of visits
               $("p.counter span").append(visited);
               // set new cookie value to match visits
               $.cookie('visited', visited, {
                   expires: 3// expires after one day
               });
               return false;
       ```
   
 * I do not need to change?
 * For me the only one place to change?
 * Because, after amendment does not work for some reason no one.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘JQuery and cookie usage.’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 2 participants
 * Last reply from: [Mantasve11](https://wordpress.org/support/users/mantasve11/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/jquery-and-cookie-usage/#post-6314924)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
