一次性弹出框 (SweetAlert2)

One-Time Pop-Up Box (SweetAlert2)

我一直在使用一个名为 SweetAlert2 的新弹出框系统,它使用了 Java 和 CSS。它看起来对用户很有吸引力,我想在我的网站上使用它来达到我正在努力实现的目的。我想创建一个一次性弹出框,一旦关闭,用户将不会再看到它。我知道很多网站都使用 cookie,但我不知道如何将其实现到我的脚本中。 我希望得到一些支持!

<script>
  swal({
    title: 'Website Maintenance Complete!',
    text: 'For full update log please click <a href="">here</a>',
    type: 'success',
    confirmButtonText: 'Nice!'
  }) 
</script>

以上是我用来在页面加载时弹出框出现的脚本,我只希望它出现一次。如果有人能指出我正确的方向或提供一个很好的解决方案。干杯!

您可以使用 cookie 检测回访者:

if (-1 === document.cookie.indexOf('returning=true')) {

  // run only if cookie not found (-1 means not found)

  swal( ... ); // alert
  document.cookie = 'returning=true'; // set cookie
}

Cookie 不是你去这里的方式, 就像@iownthegame 建议的那样,您应该使用服务器 session 对象。 会话将持续到用户离开网站,所以我认为这是正确的方法。 我不知道你的服务器端语言是什么,所以我会写一个伪:

  user login to site
  swal( ... ); // alert will popup
  when the user first login, if(session("visited")!=null){
    server should save a "flag", session("visited")=true
  }
 else{don't show the alert box}
  • cookies(不是那个问题),你应该看看here,非常有用的插件。

希望这对您有所帮助

另一个选择是使用 localStorage / sessionStorage 如果它只是客户端的东西。否则,该 cookie 将被发送到所有其他请求。

if (!localStorage.returning) {
    // run only if returning not stored in localStorage

    swal( ... ); // alert
    localStorage.returning = true; // set returning
}