0

I am creating cookies using javascript with the cookie name starting with SC_

For example, the following are some of the cookie names (there can be hundreds of such cookies):

  • SC_item1
  • SC_hello
  • SC_23
  • SC_a
  • SC_b

Now I want to read/search all cookies (in Javascript) which are starting with prefix SC_. How can I do this?

Below is the code I am using to create cookies;

function addToCart(itemid, quantity)
{
    var cookieName = "SC_"+itemid;
    var value = "hello world";
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + 365);
    var cookieValue= escape(value) + ((exdate==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie = cookieName + "=" + cookieValue;
}
sumit
  • 10,317
  • 24
  • 64
  • 81
  • 2
    Possible duplicate of: http://stackoverflow.com/questions/6088657/javascript-cookies-get-by-regex-name – jfriend00 Aug 09 '11 at 21:09
  • 1
    Do you know that each of these hundreds of cookies will be sent with every request you make to the server. Why do you waste bandwidth like this? There is a fundamental flaw in your design, IMHO. – JB Nizet Aug 09 '11 at 21:23
  • I am storing shopping cart items in cookies. And this looks like the only option. The reason I said there can be hundred of items is because right now I have not restricted the no. of items in the cart. Thanks for the suggestion JB Nizet, I will check how can this be improved. – sumit Aug 09 '11 at 21:26
  • 1
    Store the cart items in the database, at server side. And store the ID of the cart in a single cookie, or in the PHP session. – JB Nizet Aug 09 '11 at 21:36

0 Answers0