Search code examples
jquery-cookie

jquery cookie issue - does not open modal window


I have small issue with jquery-cookie plugin, as you can see in functions.php , first I register script, then enqueue jquery_cookie_script, but does not work properly... I want to archive when user open website today, and scroll.Top() > 50% of window height, modal window will be opened. When close modal window by click on the X button, will not be able to open it again after refresh the whole page, only for 7 days.

You can see my code below, I need some help how to fix that issue, because does not work at all.

jQuery(document).ready(function($) {

  $(window).scroll(function(event) {
    var y = $(this).scrollTop();
    var half_height = $(window).height() / 2;
    if (y > half_height) {
      if ($.cookie('modal_shown') == null) {
        $.cookie('modal_shown', 'yes', {
          expires: 7,
          path: '/'
        });
        $('#modal').reveal();
      }
    }
  });
});
in functions.php 
function enqueue_custom_js(){
	wp_enqueue_script('custom', get_stylesheet_directory_uri() . '/js/custom.js',array(),time());
  wp_register_script( 'jquery_cookie_script', 'http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js',array(),time());
  wp_enqueue_script( 'jquery_cookie_script');
}

add_action('wp_enqueue_scripts','enqueue_custom_js');

HTML
<!-- Modal -->
<div class="modal fade" id="modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body text-center">
        <span class="title_subtitle white">Hey babe!<br>Let's link up!</span><br>
        <span class="get_off">Get 15% OFF</span><br>
        <span class="first_order white">your first order</span><br>
        <span class="arrivals white">new arrivals, contests and promos to your inbox</span><br>
        <input type="email" name="email" placeholder="Email" required>
        <button class="btn subscribe-btn" value="Submit">Join Now</button>
      </div>
      <div class="modal-footer text-center">
        <a data-dismiss="modal" title="No thanks, I don't need a Promo Code">No thanks, I don't need a Promo Code</a>
      </div>
    </div>
  </div>
</div>


Solution

  • I found a solution that works properly

    jQuery( document ).ready( function( $ ) {
    
    $(document).on('scroll', function() {
      var y = $(this).scrollTop();
      var half_height = $(window).height()/2;
      if (y > half_height) {
        $(document).off('scroll');
    
            if ($.cookie('pop') == null) {
                $('#modal').modal('show');
            $.cookie('pop', '7');
            }
        }    
    });
    

    });