0

I have this modal popup (from topModal) on a website (i know they're annoying but my client insisted!) that pops up after 3 seconds of landing on the page.

Is there a way I can have it so it only pops up after the first visit? ...so if the user goes back to the homepage it won't popup again. If so, could someone please explain how this is done please (I'm still learning)?

;( function( $, window, document, undefined ) {

  'use strict';

  var pluginName = 'topmodal',
      defaults = {
        topmodal:             '.js-topmodal',
        topmodalBtn:          '.js-topmodal-btn',
        topmodalBtnClose:     '.js-topmodal-btn-close',
        topmodalContainer:    '.js-topmodal-container',
        topmodalOverlay:      '.js-topmodal-overlay'
      };

  function Plugin ( element, options ) {
    this.element = element;
    this.settings = $.extend( {}, defaults, options );
    this._defaults = defaults;
    this._name = pluginName;
    this.init();
  }

  $.extend( Plugin.prototype, {
    init: function() {
      var _obj = this.settings;
      Plugin.prototype.handlerMethods( _obj );
    },

    handlerMethods: function( _obj ) {
      setTimeout(function() {
        Plugin.prototype.show( _obj );
        Plugin.prototype.showContainer( _obj );
        Plugin.prototype.showOverlay( _obj );
      }, 3000); //set time here now 3 seconds

      $( document ).on( 'click', _obj.topmodalBtn, function() {
        Plugin.prototype.show( _obj );
        Plugin.prototype.showContainer( _obj );
        Plugin.prototype.showOverlay( _obj );
      });

      $( document ).on( 'click', _obj.topmodalBtnClose, function() {
        Plugin.prototype.hide( _obj );
        Plugin.prototype.hideContainer( _obj );
        Plugin.prototype.hideOverlay( _obj );
      });

      $( document ).on( 'click', _obj.topmodalContainer, function() {
        Plugin.prototype.hide( _obj );
        Plugin.prototype.hideContainer( _obj );
        Plugin.prototype.hideOverlay( _obj );
      });

      $( document ).on( 'click', _obj.topmodal, function( event ) {
        event.stopPropagation();
      });
    },

    show: function( _obj ) {
      $( _obj.topmodal ).addClass( 'is-open' );
    },

    hide: function( _obj ) {
      $( _obj.topmodal ).removeClass( 'is-open' );
    },

    showContainer: function( _obj ) {
      $( _obj.topmodalContainer ).addClass( 'is-open' );
    },

    hideContainer: function( _obj ) {
      $( _obj.topmodalContainer ).removeClass( 'is-open' );
    },

    showOverlay: function( _obj ) {
      $( _obj.topmodalOverlay ).addClass( 'is-open' );
    },

    hideOverlay: function( _obj ) {
      $( _obj.topmodalOverlay ).removeClass( 'is-open' );
    }
  });

  $.fn[ pluginName ] = function( options ) {
    return this.each( function() {
      if ( !$.data( this, "plugin_" + pluginName ) ) {
        $.data( this, "plugin_" +
               pluginName, new Plugin( this, options ) );
      }
    } );
  };

} )( jQuery, window, document );
.topmodal {
  background: #1d1c1b;
  box-sizing: border-box;
  display: none;
  position: relative;
  padding: 20px;
  max-width: 500px;
  width: 100%;
  vertical-align: middle;
  z-index: 10000;
  border: 3px solid #7A7A7A;
  border-radius: 10px;
}

.topmodal.is-open {
  display: inline-block;
}

.topmodal-overlay {
  background: rgba(0, 0, 0, 0.8);
  display: none;
  position: fixed;
  top: -100%;
  bottom: -100%;
  left: -100%;
  right: -100%;
  cursor: pointer;
}

.topmodal-overlay.is-open {
  display: block;
  z-index: 9999;
}

.topmodal-container {
  box-sizing: border-box;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
  width: 100%;
  height: 100%;
  display: none;
  text-align: center !important;
  overflow: auto;
  padding: 10px 10px 0;
}

.topmodal-container.is-open {
  display: block;
}

.topmodal-container:after {
  content: '';
  display: inline-block;
  margin-left: -.05em;
  height: 100%;
  vertical-align: middle;
}

input.button_popup_sub {
  display: block;
  text-decoration:none;
  padding:12px 20px;
  background-color:#006bb6;
  color:#1d1c1b;
  margin:10px auto 0px auto;
  font-size:1em;
  -webkit-transition: all .3s;
  transition: all .3s;
  -webkit-appearance:none;
  border:0;
  cursor:pointer;
  text-transform:uppercase;
  font-family: 'Open Sans', sans-serif;
  font-weight:bold; 
  width: 100%;
  border-radius: 5px;
}

button.js-topmodal-btn-close {
  display: block;
  text-decoration:none;
  padding:10px 20px;
  background-color:#3E3E3E;
  color:#1d1c1b;
  margin:10px auto 0px auto;
  font-size:.8em;
  -webkit-transition: all .3s;
  transition: all .3s;
  -webkit-appearance:none;
  border:0;
  cursor:pointer;
  text-transform:uppercase;
  font-family: 'Open Sans', sans-serif;
  font-weight:bold; 
  width: 90%;
  border-radius: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE HTML>
<html>

  <head>
    <title>test</title>
  </head>

  <body>



    <div class="topmodal-container js-topmodal-container">
      <div class="topmodal js-topmodal" data-modal="a">
        <div id="mc_embed_signup">
          <form action="//sklz.us12.list-manage.com/subscribe/post?u=1c357725f18822f30cba9cbf2&amp;id=3b6b7bd177" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
            <div id="mc_embed_signup_scroll">
              <label style="margin-bottom:20px;" for="mce-EMAIL">Join the Unicorn Australia mailing list</label>
              <input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" placeholder="email address" required>
              <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
              <div style="position: absolute; left: -5000px;" aria-hidden="true">
                <input type="text" name="b_1c357725f18822f30cba9cbf2_3b6b7bd177" tabindex="-1" value="">
              </div>
              <div class="clear">
                <input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button_popup_sub">
              </div>
            </div>
          </form>
        </div>
        <button class="js-topmodal-btn-close">Close</button>
      </div>
    </div>
    <div class="topmodal-overlay js-topmodal-overlay"></div>

    <!--scripts-->


    <script>
      $(document).ready(function () {
        $('.js-topmodal--log').topmodal({
          topmodal:         '.js-topmodal--log',
          topmodalBtn:      '.js-topmodal-btn--log',
          topmodalBtnClose: '.js-topmodal-btn-closesss'
        });

        $('.js-topmodal--reg').topmodal({
          topmodal:          '.js-topmodal--reg',
          topmodalBtn:       '.js-topmodal-btn--reg'
        });

        $('.js-topmodal').topmodal();
      });
    </script>


  </body>

</html>
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
DaveP19
  • 167
  • 3
  • 16

1 Answers1

0

You have to write a data to compare with and decide to instantiate or not the "topmodals".

if(localStorage['visited']!='yes'){    // This is the check

  console.log("Not visited yet... Showing modal.");

  $('.js-topmodal--log').topmodal({
    topmodal:         '.js-topmodal--log',
    topmodalBtn:      '.js-topmodal-btn--log',
    topmodalBtnClose: '.js-topmodal-btn-closesss'
  });

  $('.js-topmodal--reg').topmodal({
    topmodal:          '.js-topmodal--reg',
    topmodalBtn:       '.js-topmodal-btn--reg'
  });

  $('.js-topmodal').topmodal();

}else{
  console.log("Visited... Not showing modal.");
}

// Write the localStorage value for next visit...
localStorage['visited'] =  'yes';
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
  • Thanks for this. Im a bit out of my depth with this one I think so I might have to go and find a new plugin where its already included. – DaveP19 Aug 18 '17 at 06:35
  • I doubt you find a modal plugin which include "memory". And that is 2 lines for you to add. – Louys Patrice Bessette Aug 18 '17 at 15:14
  • I dont suppose you would want to apply it to the existing code for me...? I understand who it works etc., I'm just not sure exactly how to write it in. – DaveP19 Aug 21 '17 at 03:37
  • Did you even try? Don't you recognize the script in the `if` part? I just added the if/else... It should be really easy for you to *write it in* between the `$(document).ready(function(){` and `});` – Louys Patrice Bessette Aug 21 '17 at 03:46
  • Right - sorry. That's why I couldn't work it out. I thought the code needed to be added to the modal script. I didn't realise it was the script on the page itself that needed it. Ive done it - thanks a lot for you help. – DaveP19 Aug 21 '17 at 04:51