开发者

Javascript: Create and Check for 2 Cookies [closed]

开发者 https://www.devze.com 2023-03-23 06:24 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

Ok, we run a website that has a shopping cart. Here's the scenario: a customer comes to the site and adds a product to their cart. They go to dinner and leave the item in their cart without checking out. The next day they want to buy the item. They go to the website and, without noticing it's in their cart already, attempt to add it to the cart again where they receive an error saying it's out of stock (because there is only one in stock and they have it in their cart already.) Now we lose a sale.

What I am trying to do is create 2 cookies: one that lasts 7 days (same as the cart cookie) and one for the session. The way it works is this: their first visit it creates 2 cookies: one for 7 days and one for the session. Now lets say the customer adds a product and closes their browser. The session cookie expires, leaving the 7 Day cookie there. Now when they come back, the script will check that the 7 Day Cookie is present, but not the session cookie, triggering some of my own code to be run.

The basic structure would be like this.

If 7DayCookie Exists {
   If SessionCookie Exists {
      End Script;
   }
   Else if SessionCookie Does Not Exist {
      [Insert My Own Code]
   }
}

Else if 7DayCookie Does not Exist {
   Create SessionCoo开发者_运维百科kie;
   Create 7DayCookie;
   End Script;
}

Anybody able to make this for me? I assume it'll be a cinch for anybody that is very good with cookies and javascript.

Thanks in advance!


Final working code.

var wc = readCookie('wfwc');
var sc = readCookie('wfsc');

if (wc) {
   if (sc) { }
   else if (!sc) {
       alert("It works.");
   }
}

else if (!wc) {
   createCookie('wfwc','week',7);
   createCookie('wfsc','session',0);
}


I highly recommend the cookie functions from Peter Paul Koch's quirksmode. The 3 functions you need are createCookie, eraseCookie, and readCookie.

For the session cookie, you'll want to create a cookie that contains no "expires" header. for the 7 day cookie, you'll want to create a cookie that expires in 7 days.

Then, in javascript, you can do something like:

theme = readCookie("theme");
// if a cookie called "theme" isn't present, initialize.
if (!theme) {
    theme = "sky.json";
} 

I use PPK's scripts myself, I did change the == to === to avoid type coercion in the functions, although it's not strictly necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消