1

Currently I have this code in my Wordpress theme:

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

 if ($('body').hasClass('post-template-default')) {
    if(screen.width <= 991) {
        setTimeout(function(){ 
              swal({
        type: 'info',
        title: 'You can swipe!',
        text: 'Swipe to Right or Left to navigate through posts.',
        showConfirmButton: 'false',}) },1000); // alert }}});   

But I want it to run just once and afterwards it should not be displayed anymore in the website.

How can it be done? :)

Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
bluemask11
  • 13
  • 3

2 Answers2

0

Save data to browser if the popup is once viewed. here is working example based on the code you have provided. Here I have used sessionStorage.

if (!localStorage.getItem("popup")) {
  if ($('body').hasClass('post-template-default')) {
    if(screen.width <= 991) {
        setTimeout(function(){ 
          swal({
          type: 'info',
          title: 'You can swipe!',
          text: 'Swipe to Right or Left to navigate through posts.',
          showConfirmButton: 'false',}) 
          },1000
      ); 
  }
  localStorage.setItem("popup", 'viewed');
}
Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
  • The code works. But can it be set to not be displayed even after user leaves the browser? – bluemask11 Apr 04 '18 at 15:35
  • Use `localStorage` instead of `sessionStorage` – Albert Einstein Apr 04 '18 at 23:28
  • Tried with localStorage but the popup doesn't show up at all. the code looks exactly as above but instead of sessionStorage it's localStorage. – bluemask11 Apr 05 '18 at 05:51
  • I know but it doesn't show not even once, cleared browser chache and all data in case it was stored previously but with no luck. – bluemask11 Apr 05 '18 at 07:22
  • check the above code once, you might have missed any bracket. – Albert Einstein Apr 05 '18 at 08:14
  • The only thing i can think of as the code looks good to work is perhaps `popup` is set in storage? So i would double check its 100% not set! Perhaps double check the code in case spelling errors Or even better, perhaps change `popup` to something else.. Like `test1` and give it a rattle. Even better, maybe do a check for the correct value in case you have `popup` set elsewhere? `if(localStorage.getItem("popup") == "viewed")` ? – William Apr 05 '18 at 08:17
  • Please have a read of: https://developer.mozilla.org/en-US/docs/Web/API/Storage As this details some information regarding why it may not work? Browser compatible, usage of `Window.localStorage` etc. This may help! Maybe give window.localstorage a go? It may be an issue browser side. Do some demo DEBUG code up using the localstorage.. Have it alert you with "WELCOME" if its not set, now set the value after. If its not set, show welcome.. Else, do nothing. Simple debug just to make sure localStorage is working correctly for you :) – William Apr 05 '18 at 08:22
0

the code works like a charm, it was not working because it was inserted by me in a if ( !is_user_logged_in() ){ ?> <script> ... </script> <?php } function. thank you for you answer and time :)

bluemask11
  • 13
  • 3