View Full Version : Cookies

08-22-2011, 01:02 PM
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.

08-22-2011, 01:09 PM
sample code (http://www.quirksmode.org/js/cookies.html) to set, read and delete cookies

08-22-2011, 02:19 PM
thanks. So now i have something like this

<script type="text/javascript">

function getCookies ()

function setCookies ()
createCookie(counter1, count, 14)
createCookie(counter2, note, 14)

function createCookie(name,value,days) {
if (days) {
var date = new Date();
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;
<body onLoad="getCookies()" onUnload="setCookies()"

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?

09-04-2011, 02:56 PM
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.