仅首次显示jQmodal窗口

时间:2011-06-15 07:13:49

标签: javascript jquery jquery-plugins jqmodal

我正在使用jQmodal插件,显示弹出窗口,欢迎来到网站。

但问题是每次弹出页面刷新窗口。

这是我的代码http://jsbin.com/atoqe5/3/edit

我认为可以使用Cookie完成,但不是很多想法如何使用它。 :(

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用JavaScript设置Cookie,并在第一次打开时将其设置为true。

这些只是用于设置和获取cookie值的辅助函数more info about setting and getting cookie values

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);
    }
});

答案 1 :(得分:0)

如果您使用的是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,这样您就可以确定用户已经访问过该页面:) 希望这有帮助

相关问题