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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with cookies?

    Consider the following code, which creates, reads, and deletes a cookie that will probably not expire in the users lifetime, unless he tells it to:

    Code:
    //this code will go in a script in the head section
    function createCookie(name,value,days) {
    	days=99999;
    	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;
    }
    
    function eraseCookie(name) {
                 createCookie(name,"",-1);
    }
    How can I implement it in forms, so that a user can type in his name, and it will apear on multiple pages where I want it to? Also, how can I store images in a cookie(If I can?) If some one can give me a link to a place that teaches me alot of ways to implement cookies like this I would greatly appreciate it.
    Last edited by cjross; 07-14-2007 at 02:43 AM. Reason: removed comments

  • #2
    Regular Coder kosstr12's Avatar
    Join Date
    Apr 2007
    Location
    Woodstock, Georgia
    Posts
    428
    Thanks
    7
    Thanked 5 Times in 5 Posts
    Try http://w3schools.com/js/js_cookies.asp It helps alot i think

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thnx, I'll check it out.

    EDIT: That only tells me how to do what the code above does already. I know how to create and read the cookie, but I want the user to submit information that is turned into a cookie, and other ways of using a cookie set either by the user or me to do different things on a website.
    Last edited by cjross; 07-14-2007 at 02:54 AM.

  • #4
    Regular Coder kosstr12's Avatar
    Join Date
    Apr 2007
    Location
    Woodstock, Georgia
    Posts
    428
    Thanks
    7
    Thanked 5 Times in 5 Posts
    o, all i can think of is to do a google search for it

  • #5
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I looked on google, and found nothing.

    Funny, everyone can tell you how to create them, but not to implement them. So I came here hoping for answers.

  • #6
    New Coder
    Join Date
    Sep 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How many users will this be for?

  • #7
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This will be for everyone who uses the page.

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Here is a form using a cookie:
    Code:
    <script type="text/javascript">
    var cook=document.cookie.split("; ");
    var now=new Date();
    var exp=new Date(now.getTime()+365*24*60*60*1000);
    function getCookie(cookname) {
     if (! cook) {  return false; }
      for (var i=0; i<cook.length; i++) {
        chip=cook[i].split("=");
        if (chip[0] == cookname) {
          return unescape(chip[1]);
        }
      }
      return "";
    }
    function setCookie(cookname, cookval) {
      if (cookval != null && cookval != "") {
        document.cookie=cookname+"="+escape(cookval)+"; expires="+exp.toGMTString();
        cook=document.cookie.split("; ");
      }
    }
    function LoadCookie(){
    document.my_form.user.value=getCookie('User');
    document.my_form.txt.value=getCookie('Txt');
    }
    window.onload=LoadCookie;
    </script>
    
    
    <form name="my_form" method="post" action="" enctype="multipart/form-data">
    Name: <input type="text" name="user" value="" size="" maxlength="50" onchange="setCookie('User',this.value)" /><br />
    Image: <input type="text" name="txt" value="" size="" maxlength="200" onchange="setCookie('Txt',this.value)" /><br />
    <form>
    Last edited by rwedge; 07-15-2007 at 04:59 AM.

  • #9
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow, thanks!!!

    Anything else that I can do with cookies?
    Last edited by cjross; 07-15-2007 at 10:10 PM.


  •  

    Posting Permissions

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