簡體   English   中英

如何在第二次訪問WordPress頁面時隱藏引導程序模式

[英]how to hide bootstrap modal on second visit of a wordpress page

我正在以Bootstrap為主題的wordpress中的頁面。 頁面加載上有一個模態窗口,上面有一個注冊表單。用戶無法提交表單就看不到內容。我的問題是,當evr用戶返回該頁面時,它會顯示模態窗口,我想將其限制為一次給用戶。我怎么能做到這一點。

這是我的模態Js:

<script type="text/javascript">
jQuery(document).ready(function($) {
    $('#DownloadSignup').modal('show');
});
</script>

這是我嘗試過的方法,但無法正常工作:

<script>
jQuery(document).ready(function($) {
    var isshow = localStorage.getItem('status');
  // alert(isshow);
    if (isshow == null) {
        localStorage.setItem('isshow', 1);

        // Show popup here
        $('#DownloadSignup').modal('show');
    } else if(isshow != null) {
       $('#DownloadSignup').modal('hide');
    }
});
</script>

您需要為此使用緩存會話

如果還希望非登錄用戶使用它,則應使用緩存。 每次訪問站點時,都需要檢查緩存是否存在於系統中(如果已設置),則不要顯示模式。 這些將在第一次訪問時設置,以便在此之后的每次訪問都已經存在,從而隱藏了模式。

如果需要登錄用戶使用它,則應使用會話,遵循與上述相同的邏輯。 在會話中設置一個值,並在顯示模式之前檢查它是否存在。

改變這個

localStorage.setItem('isshow', 1);

localStorage.setItem('status', 'shown');

在您的頁面中使用此代碼,希望對您有所幫助。

<script>
    if (!localStorage['someName']) {
        localStorage['someName'] = 'yes';
        myFunction();
    }
</script>

我想如果您檢查是否設置了本地存儲,則您沒有正確檢查它,那么您必須檢查其類型和值,如下所示...

var = localStorage.getItem('status');
if (localStorage.getItem("isshow") === null) {
   //...
} 

您是否嘗試過使用jQuery Cookies。 您可以使用此庫https://github.com/carhartl/jquery-cookie

$.cookie("Show", 1, {
   expires : 1,           //expires in 1 day

   path    : '/',          //The value of the path attribute of the cookie 
                           //(default: path of page that created the cookie).

   domain  : 'yourdomain.com',  //The value of the domain attribute of the cookie
                           //(default: domain of page that created the cookie).

   secure  : true          //If set to true the secure attribute of the cookie
                           //will be set and the cookie transmission will
                           //require a secure protocol (defaults to false).
});

如果您對JQuery不感興趣,請查看有關如何使用本機JS進行設置的其他答案。 https://stackoverflow.com/a/1460174/1411055

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM