Home>

Here we use javascript cookie technology to control the prompt div on the webpage to be displayed only once.This is only displayed when the user opens the webpage for the first time.The second time it was automatically hidden,Improved the user experience.Does not annoy the user;We can do a lot more with cookies,Feel it slowly.

The screenshot of the running effect is as follows:

The online demo address is as follows:

The specific code is as follows:

<! Doctype html public "-//w3c //dtd xhtml 1.0 transitional //en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<title>cookie, div prompt box displayed only once</title>
<style>
.note {height:100px;width:600px;background:url (images/tishi.jpg) #feffcf 40px 0px no-repeat;border:#edce7b 1px solid;}
.guanbi {float:right;font-size:12px;padding:6px;}
</style>
<script>
function cookiesave (n, v, mins, dn, path)
{
 if (n)
 {
  if (! mins) mins=365 * 24 * 60;
  if (! path) path="/";
  var date=new date ();
  date.settime (date.gettime () + (mins * 60 * 1000));
  var expires=";expires =" + date.togmtstring ();
  if (dn) dn="domain =" + dn + ";";
  document.cookie=n + "=" + v + expires + ";" + dn + "path =" + path;
 }
}
function cookieget (n)
{
 var name=n + "=";
 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 (name) == 0) return c.substring (name.length, c.length);
 }
 return "";
}
function closeclick () {
 document.getelementbyid ("note"). style.display="none";
 cookiesave ("closeclick", "closeclick", "", "", "");
}
function clickclose () {
 if (cookieget ("closeclick") == "closeclick") {
  document.getelementbyid ("note"). style.display="none";
 } else {
  document.getelementbyid ("note"). style.display="block";
 }
}
window.onload=clickclose;
</script>
</head>
<body>
<div>
 <div><a href="#" onclick="closeclick ()"><img src="images/close-note.gif" border="0" /></a></div>
</div>
</body>
</html>
  • Previous PHP to achieve pagination display
  • Next What are the differences between several applications of PHP explode () function and implode () function