• 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

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    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

    Thread Starter Mantasve11

    (@mantasve11)

    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

    (@mantasve11)

    Thanks bro. Very fast support.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    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

    (@mantasve11)

    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

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    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

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Thread Starter Mantasve11

    (@mantasve11)

    } 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.