隐藏div 24小时cookie javascript?

1 html javascript cookies popup hide

我想要一个简单的JavaScript代码,允许我在单击预定义的时间内隐藏某个div元素.为了提供更多信息,我在主页加载时会出现一个建议框.我想要的是当点击div关闭按钮时,它设置一个cookie以保持盒子div关闭24小时(1天).简单地说,当按下div关闭按钮时,盒子div被隐藏24小时.注意:我有一个javascript,允许关闭按钮关闭框,但它将加载每次刷新.


http://i.stack.imgur.com/du1pA.jpg

Koe*_*ers 12

虽然TJ Crowder在他的评论中是正确的,但是stackoverflow不是用于编写代码的......我为你编写了一些代码.这是使用jQuery的解决方案.在其中你将使用a <div id="popupDiv">...</div>作为消息,并在其中使用id为"close"的链接来关闭div.

$(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);
}
Run Code Online (Sandbox Code Playgroud)

这是一个js小提琴:http://jsfiddle.net/FcFW2/1/.运行一次然后再次运行.第二次弹出窗口没有显示.