javascript 使用Cookie隐藏元素24小时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 使用Cookie隐藏元素24小时相关的知识,希望对你有一定的参考价值。

/*
You can see an example of this in action at the following URL: http://www.tmumc.org (active at the time of writing 05/10/17).
When you first go to the site, you will see an overlay that shows up, pointing them towards the App store app.
If you close this button, it sets a cookie on the users machine.
So then, if you refresh the page, the overlay will no longer show up for 24 hours.
*/



/* This piece checks to see if a cookie has been set. If it has not been set, then it adds the "active" class to the element, so that the element shows up. */

if (!readCookie('hide')) {
	$("#element-to-show").addClass("active");
}





/* This piece sets the cookie to hide the element, once they click on the close button */
$(".close-button-selector").click(function(){
	$("#element-to-show").removeClass("active");
	createCookie('hide', true, 1)
  return false;
});
	





/* The following are standard functions that need to be added into your JS file to read/write/delete cookies */

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

以上是关于javascript 使用Cookie隐藏元素24小时的主要内容,如果未能解决你的问题,请参考以下文章

使用 Javascript 隐藏元素的下一个兄弟

JavaScript 简单使用jQuery cookie插件来显示/隐藏通知

JavaScript 在每个 cookie 5 秒后隐藏 div 加载

用cookie实现tips组件的显示或隐藏

使用jQuery检测元素是不是可见[重复]

如何使用 JavaScript 创建 Chrome 扩展来隐藏或删除页面元素?