How to display hidden div after page reload?

Asked

Viewed 74 times

0

Hello, everybody.

I am developing a page where there is a hidden button that appears after a certain time.

My code:

HTML:

<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
<div class="diminuir-video" style="padding:30.25% 0 0 0;position:relative; 
max-width: 800px;">




<iframe src="LINK_DO_VIDEO" 
style="position:absolute;top:0;left:0;width:100%;height:100%;" autoplay 
width="840" height="550" frameborder="0" webkitallowfullscreen 
mozallowfullscreen allowfullscreen id="video"></iframe>
</div>

<div class="show--div-20sec">
    <p><a href="https://google.com" target="_blank" 
class="btn-01">TESTE</a>
   </p>
</div>


</div>
  <script 
src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'> 
 </script>



    <script  src="js/index.js"></script>

In JS I have the function for the div to be displayed after 18:40:

$(function() {
var iframe = $('#video')[0];
  console.log()
var player = $f(iframe);

// When the player is ready, add listeners for pause, finish, and playProgress
player.addEvent('ready', function() {    
    player.addEvent('pause', onPause);
    player.addEvent('finish', onFinish);
    player.addEvent('playProgress', onPlayProgress);
});

// Call the API when a button is pressed
$('button').bind('click', function() {
    player.api($(this).text().toLowerCase());
});

function onPause() {
}

function onFinish() {
}
var time;
if (localStorage.getItem('cache') === 'true') {
  time = 0;
} else {
  time = 1120;
  localStorage.setItem('cache', 'true');
}
function onPlayProgress(data) {
  if (data.seconds >= time) {
    $('.show--div-20sec').css('display', 'block')
  }
}
});

What I would need now is that when the user reloads the page, instead of waiting 18:40 to reappear the button, it displays immediately.

Thank you!

  • Rewrite your question, as it gets too wide.

  • Have you ever started anything? If yes, edit the question and put how far you’ve come. abs

  • @Brunoangeoletto I just edited the question, thank you so much for the feed back!

  • You can use Localstorage and store the information.

1 answer

1


Use try to use this...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="show-div-20sec" style="display: none;">
<p><a href="https://google.com" target="_blank"
      class="btn-01">TESTE</a>
</p>
</div>
<script>
if (localStorage.getItem('cache3') === 'true') {
    $('.show-div-20sec').css('display','')
} else {
    setTimeout(function(){
        $('.show-div-20sec').css('display','')
    }, 20000);
    localStorage.setItem('cache3', 'true')
}
</script>

  • Zero, I just tried to put this parameter that you informed me about, but it didn’t work. I edited my question with the full JS, can check if inserted in the correct way?

  • You want him to show up after 20,000 seconds?

  • Zero, your answer helped me. It all worked out.

  • Another question, instead of the cache, how could you consider the cookies on the page?

  • With cookie is a little more complicated, it serves to maintain connection with the client, in the case between server and client. In your case it is better to use the cache. But to use cookie in javascript is easy, document.cookie = {seu cookie}, you can save yours in GET format, sending pattern by URL {http://www.meusite.com.br/index.php?categoria=3&pag=2&tipo=5} See: https://www.w3schools.com/js_cookies.asp , https://developer.mozilla.org/en-US/docs/Web/API/Documentcookie

  • Example: document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";

  • But for you will need to read your cookie in the memento in which the user enters your site. Then you will have a role to read and understand it.

  • Got it. Thanks for the help, Zero. On my local machine it worked the validation of the cache, when I go up to the domain it does not create any cache. I’ll check on that.

Show 3 more comments

Browser other questions tagged

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