Web Analytics Made Easy -
StatCounter Cookies - CodingForum

Announcement

Collapse
No announcement yet.

Cookies

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cookies

    Hi there,
    I am writing an extension for google chrome (or at least trying to, it's my first project), and I'm kinda stuck on one part. I'd like to save 2 variables, called "c" and "notes". I'd like it to get the cookies onLoad, and save them onUnload.
    I've tried different codes, but right now I just can't figure out how to set notes and c to the value of the cookie. Could anyone please help me?
    Thanks in advance.

  • #2
    sample code to set, read and delete cookies

    Comment


    • #3
      thanks. So now i have something like this

      Code:
      <html>
      <head>
      <script type="text/javascript">
      
      function getCookies ()
      {
      readCookie(counter1)
      readCookie(counter2)
      }
      
      function setCookies ()
      {
      createCookie(counter1, count, 14)
      createCookie(counter2, note, 14)
      }
      
      function createCookie(name,value,days) {
      	if (days) {
      		var date = new Date();
      		date.setTime(date.getTime()+(days*24*60*60*1000));
      		var expires = "; expires="+date.toGMTString();
      	}
      	else var expires = "";
      	document.cookie = name+"="+value+expires+"; path=/";
      }
      
      function readCookie(name) {
      	var nameEQ = name + "=";
      	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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
      	}
      	return null;
      }
      }
      </script>
      </head>
      <body onLoad="getCookies()" onUnload="setCookies()"
      </body>
      </html>
      but how do I set count (was c, changed its name to count because there was already a c in the code, and I didn't feel like messing with it) and note as the value of the cookies called counter1 and counter2?

      Comment


      • #4
        Ok, figured out how it worked. Had to reload the extension every time I changed the manifest and/or background page. and then I ended up using localStorage (it was a lot easier to use and access. Otherwise, thanks for your kind 7 words of answer, and your attentionate replies to all of my questions.

        Comment

        Working...
        X