Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Cookies

  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 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
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    sample code to set, read and delete cookies

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •