简体   繁体   English

如何仅触发一次引导模式并使用浏览器本地存储来检测模式已被触发

[英]How to trigger bootstrap modal just once and using browser localstorage to detect the modal was triggered already

Here is my bootstrap modal code这是我的引导模式代码

    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalCenterTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
       ok so the content goes here good good
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

and the javascript code to trigger after 2 seconds once the page is loaded以及页面加载后 2 秒后触发的 javascript 代码

$(window).ready(function() {
   setTimeout(function(){
       $('#exampleModalCenter').modal('show');
   }, 2000);
});

I'm looking for a code to trigger this function just once for a user and not to keep repeating each time when they visit the page again.我正在寻找一个代码来为用户只触发一次这个功能,而不是在他们再次访问页面时每次都重复。

So, i have decided to use browser localstorage method to save this event and detect again whether it was already triggered or not.所以,我决定使用浏览器本地存储方法来保存这个事件并再次检测它是否已经被触发。

I'm open for suggestions, if there is any better way to do this.如果有更好的方法可以做到这一点,我愿意接受建议。

Thanks.谢谢。

ohk so you want to save trigger status in localStorage...哦,所以你想在 localStorage 中保存触发状态......

$(document).ready(() => {
        console.log(window.localStorage.modal);
        if (window.localStorage.modal != "true") {
            setTimeout(function(){
                $("#exampleModalCenter").modal('show');
                window.localStorage.modal = true;
            }, 2000);
        }
})

This code will check first if the modal value exist or modal value is false which in first case condition will be true so modal will be visible and localstorage modal value will be true.此代码将首先检查模态值是否存在或模态值是否为假,这在第一种情况下条件为真,因此模态将可见并且本地存储模态值为真。 After that for other cases it will not trigger because modal value !== true condition.之后,对于其他情况,它不会触发,因为 modal value !== true 条件。

note - but if they clear the localstorage it will not be able to help you.注意 - 但如果他们清除了本地存储,它将无法帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM