Jquery notification appear only 1 time

Asked

Viewed 62 times

0

How do I, according to the code below, the notification appears only once at each access?

inserir a descrição da imagem aqui

<script src="assets/plugins/notification/js/bootstrap-growl.min.js"></script>
    <script>
    'use strict';
    $(window).on('load',function(){
      function notify(message, type){
          $.growl({
              message: message
          },{
              type: type,
              allow_dismiss: false,
              label: 'Cancel',
              className: 'btn-xs btn-warning',
              placement: {
                  from: 'top',
                  align: 'right'
              },
              spacing: 10,
              z_index: 999999,
              delay: 10000,
              animate: {
                      enter: 'animated fadeInRight',
                      exit: 'animated fadeOutRight'
              }
          });
      };
          notify('<span style="color: #000;font-size: 14px; font-weight: bold">ENVIAR MENSAGEM HOJE</span>&nbsp;<button type="button" class="close" data-growl="dismiss"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>', 'warning');
    });
    </script>
  • 1

    Experimented sessionStorage?

  • Hello William, I don’t get a lot of Jquery. How could I apply sessionStorage to this code?

  • sessionStorage is not jQuery => https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

1 answer

1


As the friend Guilherme Nascimento spoke in the comments, it is possible to use the sessionStorage, recording a key, say "displayMensage" and check if its value is "true" (or "displayed" or something else, you choose) before displaying the message. It would look something like this:

if(localStorage.getItem('exibiuMensagem') != true){
    notify(...);
    localStorage.setItem('exibiuMensagem', true);
}

Thus, at each access, the if would check if this item already exists and has value 'true', if not, it would display the message and save the item.

Also, it is good to remove the item when the user closes the browser, so that next time the message appears again. You can do that with this function:

window.onbeforeunload = function{
   localStorage.removeItem('exibiuMensagem');
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.