0

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

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

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

我认为可以使用 Cookies 来完成,但不知道如何使用它。:(

谢谢!

4

2 回答 2

1

您可以使用 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);
    }
});
于 2011-06-15T07:50:07.830 回答
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,这样您就可以确定用户已经访问过该页面:) 希望这会有所帮助

于 2011-06-15T07:45:22.400 回答