仅第一次显示jQmodal窗口

Show jQmodal window for the first time only

本文关键字:窗口 jQmodal 显示 第一次      更新时间:2023-09-26

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

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

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

我认为可以使用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,这样您就可以确保用户已经访问了页面:)希望这对有帮助