Use js to implement a page floating layer effect, and use js to read and write cookies to display the status of closed advertisements. The following is a good example, if you are interested, you can refer to the following case to use js to implement a page floating layer effect. In addition, you can use js to read and write cookies in two ways to display the status of closed advertisements;
You can copy the following code to an html file to try the effect. The pre-tag of html is not implemented in two js methods.
The Code is as follows:
IT_Blog _ Yang Kai
Author: IT_Blog _ Yang Kai
Reprinted please specify the Source: http://blog.csdn.net/yangkai_hudong"> http://blog.csdn.net/yangkai_hudong </a>
1. First: Use the cookie library of jQuery
The sample code is as follows:
$ (Document). ready (function (){
Var adCookie = $. cookie ("docCookie ");
// If no cookie exists locally, write the entry cookie to the local device.
If (adCookie! = "AdDocCookie "){
$ ("# WapDocCookie"). show ();
}
// If the entry cookie exists locally, the floating layer is not displayed.
If (adCookie = "adDocCookie "){
$ ("# WapDocCookie"). hide ();
}
// Close the advertisement and hide the floating layer
$ ("# CloseAd"). click (function (){
$ ("# WapDocCookie"). hide ();
$. Cookie ("docCookie", "adDocCookie", {expires: 60 });
});
});
// JQuery cookie library
JQuery. cookie = function (name, value, options ){
If (typeof value! = 'Undefined') {// name and value given, set cookie
Options = options | {};
If (value = null ){
Value = '';
Options. expires =-1;
}
Var expires = '';
If (options. expires & (typeof options. expires = 'number' | options. expires. toUTCString )){
Var date;
If (typeof options. expires = 'number '){
Date = new Date ();
Date. setTime (date. getTime () + (options. expires * 24x60*60*1000 ));
} Else {
Date = options. expires;
}
Expires = '; expires =' + date. toUTCString (); // use expires attribute, max-age is not supported by IE
}
Var path = options. path? '; Path =' + (options. path ):'';
Var domain = options. domain? '; Domain =' + (options. domain ):'';
Var secure = options. secure? '; Secure ':'';
Document. cookie = [name, '=', encodeURIComponent (value), expires, path, domain, secure]. join ('');
} Else {// only name given, get cookie
Var cookieValue = null;
If (document. cookie & document. cookie! = ''){
Var cookies = document. cookie. split (';');
For (var I = 0; I <cookies. length; I ++ ){
Var cookie = jQuery. trim (cookies [I]);
// Does this cookie string begin with the name we want?
If (cookie. substring (0, name. length + 1) = (name + '= ')){
CookieValue = decodeURIComponent (cookie. substring (name. length + 1 ));
Break;
}
}
}
Return cookieValue;
}
};
2. Type 2: write your own js operation cookie instance
Related js:
$ (Document). ready (function (){
Function writeCookie (name, value)
{
Var exp = new Date ();
Exp. setTime (exp. getTime () + 7x24x60*60*1000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}
// Read cookies
Function readCookie (name)
{
Var arr, reg = new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )");
If (arr = document. cookie. match (reg )){
Return unescape (arr [2]);
} Else {
Return null;
}
}
Var adCookie = readCookie ("adCookie ");
If (adCookie! = "AdDocCookie "){
$ ("# WapDocCookie"). show ();
}
// If the entry cookie exists locally, the floating layer is not displayed.
If (adCookie = "adDocCookie "){
$ ("# WapDocCookie"). hide ();
}
// Close the advertisement and hide the floating layer
$ ("# CloseAd"). click (function (){
$ ("# WapDocCookie"). hide ();
});
});