簡體   English   中英

僅首次顯示jQmodal窗口

[英]Show jQmodal window for the first time only

我正在使用jQmodal插件,顯示彈出窗口,歡迎訪問網站。

但是問題在於每次頁面刷新窗口都會彈出。

這是我的代碼http://jsbin.com/atoqe5/3/edit

我認為可以使用Cookie來完成此操作,但是對於如何使用Cookie卻不多。 :(

謝謝!

您可以使用JavaScript設置cookie,並在首次打開時將其設置為true。

這些只是用於設置和獲取Cookie值的輔助函數,有關設置和獲取Cookie值的更多信息

function setCookie(name, value, daysToLive) {
    var expirationDate = new Date();
    expirationDate.setDate(expirationDate.getDate() + daysToLive);
    document.cookie = name + '=' + escape(value) + ((daysToLive == null) ? '' : '; expires=' + expirationDate.toUTCString());
}

function getCookie(name) {
    var cookies=document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
        if (cookies[i].substr(0, cookies[i].indexOf('=')).replace(/^\s+|\s+$/g, '') == name) {
            return unescape(cookies[i].substr(cookies[i].indexOf('=') + 1));
        }
    }
}

如果設置了該值,則防止打開模態:

$(function() {
    if (!getCookie('modalOpened')) {
        // Put your code to open the model here...

        // Set value to true to prevent the modal from opening again 
        setCookie('modalOpened', true);
    }
});

如果您使用的是php,則可以執行以下操作:將每一頁放在第一行

<?php session_start(); ?>

和你的主頁

<?php session_start();
  if( $_SESSION['visited'] ){
      //don't show the modal box
  } else {
      $_SESSION['visited'] = true;
     //show modal box;
  }
?>

此代碼檢查您是否已在此會話中訪問過該頁面,如果未顯示模式框,則將全局會話變量$_SESSION['visited']true ,這樣就可以確保用戶已經訪問了頁面:)希望這有所幫助

暫無
暫無

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

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