Example of advertisement display for JavaScript cookie instance application

Source: Internet
Author: User

This is a Web page special effects code A:

function Setcookie (name,value,days) {

var exp=new date ();

Exp.settime (Exp.gettime () + days*24*60*60*1000);

var arr=document.cookie.match (new RegExp (^|) +name+ "= ([^;] *)(;|$)"));

document.cookie=name+ "=" +escape (value) + "; expires=" +exp.togmtstring ();

}

function GetCookie (name) {

var arr=document.cookie.match (new RegExp (^|) +name+ "= ([^;] *)(;|$)"));

if (arr!=null) {

Return unescape (arr[2]);

return null;

}

}

function Delcookie (name) {

var exp=new date ();

Exp.settime (Exp.gettime ()-1);

var cval=getcookie (name);

if (cval!=null) {

document.cookie=name+ "=" +cval+ "; expires=" +exp.togmtstring ();

}

}

The above code is the function of reading and writing cookies, which you do not need to understand, as long as you know how to use on the line


MJJ look at the following page special effects code, the code saved as pacth.js in the same Web page call, note that first call JS code A, and then call JS Code b
<script src= "Cookies.js" ></script><script src= "Patch.js" ></script>
Attention to the path problem, this is not much to say

This is JS code b

function out () {

if (GetCookie ("Out")!= "yes") {

Setcookie ("Out", "yes", 1);d ocument.write (' Place your JS code here-Callout 1 ');

}

}



Out ();
The code here is to call the function in the cookie to determine if the cookie named out already exists, and if not, set the cookie to Yes
And then run your JS code.

At this point, if you refresh the page, the second named out cookies already exist, you will not run the code labeled 1

Add:

What if I want to set up a window and have more code to set the time?
Then rename the function out () and set up a function to control the time.

I'm afraid I'm not sure.

Let me give you an example

function out1 () {

if (GetCookie ("out1")!= "yes") {

Setcookie ("Out1", "yes", 1);d ocument.write (' Place your JS code 1-Callout 1 ');

}

}

OUT1 ();
[Code]function Out2 () {
if (GetCookie ("Out2")!= "yes") {
Setcookie ("Out2", "yes", 1);d ocument.write (' Place your JS code 2-Callout 1 ');
}
}[
Out2 ();

/code] by analogy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.