弹出应该每次访问显示一次,而不是每次显示

时间:2018-05-11 17:00:46

标签: javascript

我希望我的弹出窗口每次访问只显示一次。我究竟做错了什么?每次刷新浏览器时都会显示。

这就是我所拥有的。

<body onload="myFunction()">
  <!-- Modal -->
  <div id="ac-wrapper" style='display:none'>
    <div id="popup">
      <center>
        <h3>DISCLAIMER</h3>
        <input type="submit" name="submit" value="I AGREE" onClick="PopUp('hide')" id="button" />
      </center>
    </div>
  </div>

  <script>
    var cookie = localStorage.getItem('myPopup');
    if (!cookie) {
      function PopUp(hideOrshow) {
        if (hideOrshow == 'hide') document.getElementById('ac-wrapper').style.display = "none";
        else document.getElementById('ac-wrapper').removeAttribute('style');
      }
      window.onload = function() {
        setTimeout(function() {
          PopUp('show');
        }, 1000);
      }
      localStorage.setItem('myPopup', 'true');
    }
  </script>
</body>

1 个答案:

答案 0 :(得分:0)

在window.onload函数中移动If(!cookie)

<body >
            <!-- Modal -->
        <div id="ac-wrapper" style='display:none'>
                <div id="popup">
                        <center>
                                 <h3>DISCLAIMER</h3>
                                 <input type="submit" name="submit" value="I AGREE" onClick="PopUp('hide')" id="button" />
                        </center>
                </div>
        </div>

        <script>
            var cookie = localStorage.getItem('myPopup');
            function PopUp(hideOrshow) {
                if (hideOrshow == 'hide') document.getElementById('ac-wrapper').style.display = "none";
                else document.getElementById('ac-wrapper').removeAttribute('style');
            }
            window.onload = function () {
                if(!cookie){
                        setTimeout(function() {
                  PopUp('show');
                }, 1000);        }

            }
            localStorage.setItem('myPopup','true');
        </script>
    </body>