【问题标题】:Show a DIV based on sessions until the user closes it显示基于会话的 DIV,直到用户关闭它
【发布时间】:2018-10-31 17:42:31
【问题描述】:

我正在尝试向用户显示一个 DIV,直到他/她关闭它。一旦用户点击close,DIV 将保持关闭 24 小时。

这是每 24 小时显示一次的工作代码。但是,我不确定如何添加点击功能:

<?php
    if (!isset($_COOKIE['cookie'])) {
        setcookie('cookie', true, time() + 3600 * 24); // Save a cookie for 1 day
        echo '<div class="slideshow"><span class="close">close</span>Hello World</div>';
    }
?>

【问题讨论】:

  • 您在slideshow div 中添加一个按钮,该按钮调用一个JS 函数,该函数又从DOM 中删除sideshow div。

标签: php session cookies session-cookies


【解决方案1】:

你应该试试这样的。

$(document).ready(function() {

  // If the 'hide cookie is not set we show the message
  if (!readCookie('hide')) {
    $('#popupDiv').show();
  }

  // Add the event that closes the popup and sets the cookie that tells us to
  // not show it again until one day has passed.
  $('#close').click(function() {
    $('#popupDiv').hide();
    createCookie('hide', true, 1)
    return false;
  });

});

// ---
// And some generic cookie logic
// ---
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}

代码取自这里。SEE

【讨论】:

  • 谢谢。玩弄它以在 PHP 中完成它。将您的答案标记为正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多