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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    172
    Thanks
    64
    Thanked 2 Times in 2 Posts

    processing cookies

    I am trying to prompt user to enter a name into a form, then save it as a cookie and display it in a sentence at the end. Enter - John -> cookie -> Hello John or something like that...

    Here is the code for 2 functions:

    Code:
    <script type="text/javascript">
    
    function save_cookie() {
    	var entry=window.prompt("What is your name?", "");
    	var name="cNam="+entry;
    	var escpd=escape(name);
    	var xDte=";expires=Mon, 17 Oct 2011 13:00:00 UTC";
    	escpd+=xDte;
    	document.cookie=escpd;
    }
    
    function display_cookie() {
    	if (document.cookie) {
    		var rd=document.cookie;
    		var sPlit=rd.split("=");
    		var frst=sPlit[0];
    		var scnd=sPlit[1];
    		window.alert=("Welcome"+scnd);
    		}
    		else {
    			save_cookie();
    			}
    	}
    
    save_cookie();
    display_cookie();
    </script>
    The code seems to be ok for the most part, but I get something like that when I test it on every line with window.alert


    cNam%3Djohn;
    mediaWiki.user.bucket:ext.articleFeedback-tracking=8%3Aignorel
    mediaWiki.user.bucket:ext.articleFeedback-options=8%3Ashow;
    wp-settings-time-1=1318110074;wp-settings-1=editor%3Dtinymce


    The first line is ok, but I am not sure about the remaining ones. Does anybody knows what is it and how to get rid of that?
    I found this on StackOverflow (it is suppose to clean all cookies for particular domain, but I am not sure if it is working and I would prefer to stick to just two functions that I hace):

    Code:
    function deleteAllCookies() { 
        var cookies = document.cookie.split(";"); 
     
        for (var i = 0; i < cookies.length; i++) { 
            var cookie = cookies[i]; 
            var eqPos = cookie.indexOf("="); 
            var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; 
            document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; 
        } 
    }
    Thank you.

    PS: The first function works, the second one seems to be splitting everything ok etc, it is just these 3 lines that I need to get rid of and I am not sure what it is too (prompt entry "john", I wanna get to "Welcome john" with functions that I already have)
    Last edited by joliett89; 10-10-2011 at 04:08 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    If you are on a shared account on the same domain as other users you should think twice about clearing everyone else's cookies, and instead target only your cookies.

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Posts
    172
    Thanks
    64
    Thanked 2 Times in 2 Posts
    I think its some kind of cookies from the free hosting account that I am testing it on (000webhost)
    Last edited by joliett89; 10-10-2011 at 04:22 PM.

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Yeah. The cookies (as I understand them) are a collection of all the cookies used by all the sites that a person visits. So when I first learned how to use cookies when I tried to print the cookies I got all the cookies. Seems to me like it would be a common beginners mistake...

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Cookies are tough... I would use a cookie handling script to do the dirty work, so that I could just treat them like any other variable... try this http://techpatterns.com/downloads/ja...pt_cookies.php

  • #6
    Regular Coder
    Join Date
    Jul 2011
    Posts
    172
    Thanks
    64
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by blaze4218 View Post
    Yeah. The cookies (as I understand them) are a collection of all the cookies used by all the sites that a person visits. So when I first learned how to use cookies when I tried to print the cookies I got all the cookies. Seems to me like it would be a common beginners mistake...
    var example=document.cookies
    window.alert(example)

    That should be all the cookies for the document object, I think, not all the other sites. Cookies for all the sites are (at least in Windows 7) at

    C:\Users\u s e r n a m e\AppData\Roaming\Microsoft\Windows\Cookies (I have 3,136 in the folder now), but for document.cookies I get this:

    cNam%3Djohn;
    mediaWiki.user.bucket:ext.articleFeedback-tracking=8%3Aignorel
    mediaWiki.user.bucket:ext.articleFeedback-options=8%3Ashow;
    wp-settings-time-1=1318110074;wp-settings-1=editor%3Dtinymce

    The first line is what I need...
    Last edited by joliett89; 10-10-2011 at 04:21 PM.

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Well I would still assume based on the cookies returned that you have either a shared hosting, or CMS, which is the problem that I was addressing. I only added that line for commentary. And whether it is CMS or Shared hosting, I would just consider carefully whether or not to remove those other cookies...
    The idea that I'm trying to convey here, is that you should only worry about removing cookies that you create. If you target your cookies directly, then the other ones shouldn't matter.

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    split your cookie at the ';' cycle through the array object till you find your cookie name, and extract the value of your cookie.

  • #9
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Code:
    AllCookiesInThisDoc = document.cookie.split(';');
    myCookie = 'cNam';
    
    function RetrieveMyCookie(){
     for(var i in AllCookiesInThisDoc){
      if(AllCookiesInThisDoc[i].match(myCookie)){
       myCookie = AllCookiesInThisDoc[i];
       }
      }
     if(myCookie!=='cNam');
      myCookie.split('%3D');
     }
    now you have a cookie Array that looks like ['cNam','john']

  • Users who have thanked blaze4218 for this post:

    joliett89 (10-10-2011)

  • #10
    Regular Coder
    Join Date
    Jul 2011
    Posts
    172
    Thanks
    64
    Thanked 2 Times in 2 Posts
    Thanks


  •  

    Posting Permissions

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