Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

javascript - Check if the page was reload

I have a problem with my site. So, I have a div with adsense like this :

<div class="not-display" id="addsense-pub">
   ........................................
</div>

In the .js after 2 games I execute the following javascript :

if(obj.number_games_adresse % 2 === 0){
  document.getElementById('addsense-pub').setAttribute('class','display-block');
  document.getElementById("game").style.visibility = "hidden";
 }

The div "game" is the div which contains the game.

Now if I show the adsense-pub and the user refreshes the page, the pub disappears. However I want to show this div if user refreshes the page. Is there an existing solution for this case?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I recreated your scenario. You can use sessions in this case to do this...

See the jsfiddle for this: http://jsfiddle.net/wde20oj2/

You can check the session to see if the ad was displayed. If it was go ahead and display it again.

<!DOCTYPE html>
<html>
  <head>
    <title>Some JavaScript Example</title>
    <style>
        .not-display {
            display:none;
        }
        .display-block {
            display: block;
        }
    </style>
  </head>
  <body>
    <div class="not-display" id="addsense-pub">   
        Hello... I am an add!
    </div>
    <div id="game">
        Hello... I am a game!
    </div>  
      <br>
    <a href="#" id="click-me">Increment Games...</a>
      <br><br>
    <a href="#" id="clear-me">Clear all items!!!</a>
    <!-- JavaScript -->
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script>  
        sessionStorage.setItem('games', 0);
        var data = sessionStorage.getItem('show-add');
        if(data == 1)
        {
            $("#addsense-pub").removeClass('not-display');
            $("#addsense-pub").addClass('display-block');
        }

        $("#click-me").click(function(){
            var gamer = sessionStorage.getItem('games');
            if(gamer < 1)
            {
                gamer += 1;
                sessionStorage.setItem('games', gamer);
            } else {
                $("#addsense-pub").removeClass('not-display');
                $("#addsense-pub").addClass('display-block');
                sessionStorage.setItem('show-add', 1);
            }
        });

        $("#clear-me").click(function(){
            sessionStorage.setItem('games', 0);
            sessionStorage.setItem('show-add', 0);
            window.location.reload()
        });

    </script>
  </body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...