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 13 of 13
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Need Cookie Help

    Hello. I am a neewb, so bare with me. This code is not working correctly for some reason. If I use it in Internet Explorer it will work, but only if you bring up the history in the url tab. You cannot refresh it for whatever reason. so basically it works in Explorer but no refresh. The big problem is Mozilla. I will not work at all. I have all of the cookies set for third party, remember last visit and so on. It will only display the welcome page for first time visitor. Then it will show the subsequent page, however it will not increment the count +1. I am not sure what is going on here, Explorer works, but with no refresh, and Mozilla does not really work at all? Here is my script currently:

    <script type="text/javascript">
    /* <![CDATA[ */

    function hitMySite()
    {
    var lastDate = new Date();
    lastDate.setMonth(lastDate.getMonth());
    var dateString = (lastDate.getMonth() + 1) + "/" + lastDate.getDate() + "/" + lastDate.getFullYear();
    if (document.cookie != "")
    {
    counter = document.cookie.split("=")[1];
    counter = parseInt(counter) + 1;
    date = document.cookie.split(":")[2];
    var expireDate = new Date();
    expireDate.setMonth(expireDate.getMonth() + 12);
    document.cookie = "counter=" + counter + ":date:" + dateString + ";expires=" + expireDate.toGMTString();
    document.write("<h1>You last visited this page on: " + date + "<br />You have been to this page " + counter + " times.</h1>");
    }
    else
    {
    document.write("<h1>Welcome to my Web site! This is your first visit here so be sure to bookmark my page!</h1>");
    var counter = 1;
    var expireDate = new Date();
    expireDate.setMonth(expireDate.getMonth() + 12);
    document.cookie = "counter=" + counter + ":date:" + dateString + ";expires=" + expireDate.toGMTString();
    }
    }
    /* ]]> */
    </script>
    </head>
    <body onload="hitMySite()">
    </body>
    </html>

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    if (document.cookie != "")
    {
    counter = document.cookie.split("=")[1];
    counter = parseInt(counter) + 1;
    date = document.cookie.split(":")[2];
    document.cookie may contain other data, so that's not a reliable way to recover your cookie.

    You can use whichever of these readers you find least confusing to understand.

    They're all used in the same way: var cookieValue = readCookie( 'myCookie' );

    Code:
    function readCookie( cName )
    {
     var nameIdx, cv = "", data = " " + ( document.cookie || "" ) + ";" ;  
     
     if( ( nameIdx = data.indexOf( " " + cName + '=' ) ) != -1 )
      cv = data.substring( nameIdx += cName.length + 2, data.indexOf( ';', nameIdx ) );
      
     return decodeURIComponent( cv ); 
    }
    
    function readCookie( cName ) 
    { 
      var cookieArray = document.cookie.split( /;\s*/ ),
          cookie,
          cValue = ""; 
          
      for( var i = 0; i < cookieArray.length && !cValue; i++ ) 
        if( ( cookie = cookieArray[ i ] ).indexOf( cName ) == 0 ) 
          cValue = cookie.split( "=" )[ 1 ]; 
     
      return decodeURIComponent( cValue ); 
    }
    
    
    function readCookie( cName )
    {
      var v;
    
      return decodeURIComponent( ( v = ( document.cookie || "" ).match( "(^|\\s)" + cName + "=([^;]+)" ) ) ? v[ 2 ] : "" );
    }
    Last edited by Logic Ali; 05-01-2012 at 05:03 AM.

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    document.cookie may contain other data, so that's not a reliable way to recover your cookie.

    You can use whichever of these readers you find least confusing to understand.

    They're all used in the same way: var cookieValue = readCookie( 'myCookie' );

    Code:
    function readCookie( cName )
    {
     var nameIdx, cv = "", data = " " + ( document.cookie || "" ) + ";" ;  
     
     if( ( nameIdx = data.indexOf( " " + cName + '=' ) ) != -1 )
      cv = data.substring( nameIdx += cName.length + 2, data.indexOf( ';', nameIdx ) );
      
     return decodeURIComponent( cv ); 
    }
    
    function readCookie( cName ) 
    { 
      var cookieArray = document.cookie.split( /;\s*/ ),
          cookie,
          cValue = ""; 
          
      for( var i = 0; i < cookieArray.length && !cValue; i++ ) 
        if( ( cookie = cookieArray[ i ] ).indexOf( cName ) == 0 ) 
          cValue = cookie.split( "=" )[ 1 ]; 
     
      return decodeURIComponent( cValue ); 
    }
    
    
    function readCookie( cName )
    {
      var v;
    
      return decodeURIComponent( ( v = ( document.cookie || "" ).match( "(^|\\s)" + cName + "=([^;]+)" ) ) ? v[ 2 ] : "" );
    }
    Thanks for your reply. I am very new to this and I really have not learned arrays yet, so I am having trouble understanding the script you typed. Also, where is the document.write statements for the display when the user comes to the site? Anyway thanks again for replying .

  • #4
    New Coder
    Join Date
    Apr 2012
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Yeah this is very strange. It will work in Internet Explorer not in Mozilla. Mozilla will only display the date, but not the visitor hits. In fact it displays NaN total number of visits....

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Neewb View Post
    Thanks for your reply. I am very new to this and I really have not learned arrays yet, so I am having trouble understanding the script you typed. Also, where is the document.write statements for the display when the user comes to the site? Anyway thanks again for replying .
    You're obviously out of your depth so here's the fix.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    /* <![CDATA[ */
    
    function readCookie( cName )
    {
     var nameIdx, cv = "", data = " " + ( document.cookie || "" ) + ";" ;  
     
     if( ( nameIdx = data.indexOf( " " + cName + '=' ) ) != -1 )
      cv = data.substring( nameIdx += cName.length + 2, data.indexOf( ';', nameIdx ) );
      
     return decodeURIComponent( cv ); 
    }
    
    function hitMySite()
    {
      var cv = readCookie( 'counter' ),
          visits = 1,
          dt = new Date(),
          now = new Date();
          
      if( cv )
      {
        cv = cv.split( '||' );    
          
        visits = Number( cv[ 1 ] ) + 1; 
        
        document.write("<h1>You last visited this page on: " + cv[ 0 ] + "<br />You have been to this page " + visits + " times.</h1>");
      }
      else
      {
        document.write("<h1>Welcome to my Web site!<br>This is your first visit here so be sure to bookmark my page!</h1>");
      }
    
      dt.setMonth( dt.getMonth() + 12 );
     
      document.cookie = 'counter=' + now.getDate() +'/' + (now.getMonth() + 1) + '/' + now.getFullYear()+'||'+ visits + ";expires=" + dt.toGMTString();
    }
    
    /* ]]> */
    </script>
    </head>
    <body onload="hitMySite()">
    </body>
    </html>
    Be aware that in Europe this script as it stands is now technically illegal.
    If this leads to a debate, I'm staying out of it...

  • #6
    New Coder
    Join Date
    Apr 2012
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    You're obviously out of your depth so here's the fix.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    /* <![CDATA[ */
    
    function readCookie( cName )
    {
     var nameIdx, cv = "", data = " " + ( document.cookie || "" ) + ";" ;  
     
     if( ( nameIdx = data.indexOf( " " + cName + '=' ) ) != -1 )
      cv = data.substring( nameIdx += cName.length + 2, data.indexOf( ';', nameIdx ) );
      
     return decodeURIComponent( cv ); 
    }
    
    function hitMySite()
    {
      var cv = readCookie( 'counter' ),
          visits = 1,
          dt = new Date(),
          now = new Date();
          
      if( cv )
      {
        cv = cv.split( '||' );    
          
        visits = Number( cv[ 1 ] ) + 1; 
        
        document.write("<h1>You last visited this page on: " + cv[ 0 ] + "<br />You have been to this page " + visits + " times.</h1>");
      }
      else
      {
        document.write("<h1>Welcome to my Web site!<br>This is your first visit here so be sure to bookmark my page!</h1>");
      }
    
      dt.setMonth( dt.getMonth() + 12 );
     
      document.cookie = 'counter=' + now.getDate() +'/' + (now.getMonth() + 1) + '/' + now.getFullYear()+'||'+ visits + ";expires=" + dt.toGMTString();
    }
    
    /* ]]> */
    </script>
    </head>
    <body onload="hitMySite()">
    </body>
    </html>
    Be aware that in Europe this script as it stands is now technically illegal.
    If this leads to a debate, I'm staying out of it...
    I tried your code just now, and same result on Mozilla only this time it displays the wrong date. Explorer will run the script, albeit wrong date...Thanks for your reply

  • #7
    New Coder
    Join Date
    Apr 2012
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Strange thing just occurred. The original script at the very top that I posted for this thread now works in Mozilla, however i get this garbled garbage at the end of the date message:

    You last visited this page on: 5/1/2012; visit=Tue%20May%2001%202012%2000%3A49%3A38%20GMT-0700%20%28Pacific%20Daylight%20Time%29

    Other than the garbage, it actually works, and increments the page after every refresh. I wonder if it has anything to do with me clearing my cookies last night?
    Anyway, what's with that garbage then? Is there any way to fix it in the code?
    Last edited by Neewb; 05-02-2012 at 12:49 AM. Reason: missed something

  • #8
    New Coder
    Join Date
    Apr 2012
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    The above post was for the original script that I posted when I came on here yesterday not the new one. The original works but with the garbage, the other script provides the wrong date and does not work in Mozilla. Hope that clears up any confusion.

  • #9
    New Coder
    Join Date
    Apr 2012
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Darryl197 View Post
    Yeah this is very strange. It will work in Internet Explorer not in Mozilla. Mozilla will only display the date, but not the visitor hits. In fact it displays NaN total number of visits....
    Did you try the first code that I posted originally? That one now works, but Have some garbage writing on the end of the first line. Other than that it does increment the visits.

  • #10
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Neewb View Post
    I tried your code just now, and same result on Mozilla only this time it displays the wrong date. Explorer will run the script, albeit wrong date...Thanks for your reply
    When you say the wrong date, it can only be the dd/mm/yyyy format that I used, which should be trivial for you to change, but just to be on the safe side I've done it for you. I can't think how else it could fail.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    /* <![CDATA[ */
    
    function readCookie( cName )
    {
     var nameIdx, cv = "", data = " " + ( document.cookie || "" ) + ";" ;  
     
     if( ( nameIdx = data.indexOf( " " + cName + '=' ) ) != -1 )
      cv = data.substring( nameIdx += cName.length + 2, data.indexOf( ';', nameIdx ) );
      
     return decodeURIComponent( cv ); 
    }
    
    function hitMySite()
    {
      var cv = readCookie( 'counter' ),
          visits = 1,
          dt = new Date(),
          now = new Date();
          
      if( cv )
      {
        cv = cv.split( '||' );    
          
        visits = Number( cv[ 1 ] ) + 1; 
        
        document.write("<h1>You last visited this page on: " + cv[ 0 ] + "<br />You have been to this page " + visits + " times.</h1>");
      }
      else
      {
        document.write("<h1>Welcome to my Web site!<br>This is your first visit here so be sure to bookmark my page!</h1>");
      }
    
      dt.setMonth( dt.getMonth() + 12 );
     
      document.cookie = 'counter=' + (now.getMonth() + 1) +'/' + now.getDate() + '/' + now.getFullYear()+'||'+ visits + ";expires=" + dt.toGMTString();
    }
    
    /* ]]> */
    </script>
    </head>
    <body onload="hitMySite()">
    </body>
    </html>

  • #11
    New Coder
    Join Date
    Apr 2012
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    When you say the wrong date, it can only be the dd/mm/yyyy format that I used, which should be trivial for you to change, but just to be on the safe side I've done it for you. I can't think how else it could fail.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    /* <![CDATA[ */
    
    function readCookie( cName )
    {
     var nameIdx, cv = "", data = " " + ( document.cookie || "" ) + ";" ;  
     
     if( ( nameIdx = data.indexOf( " " + cName + '=' ) ) != -1 )
      cv = data.substring( nameIdx += cName.length + 2, data.indexOf( ';', nameIdx ) );
      
     return decodeURIComponent( cv ); 
    }
    
    function hitMySite()
    {
      var cv = readCookie( 'counter' ),
          visits = 1,
          dt = new Date(),
          now = new Date();
          
      if( cv )
      {
        cv = cv.split( '||' );    
          
        visits = Number( cv[ 1 ] ) + 1; 
        
        document.write("<h1>You last visited this page on: " + cv[ 0 ] + "<br />You have been to this page " + visits + " times.</h1>");
      }
      else
      {
        document.write("<h1>Welcome to my Web site!<br>This is your first visit here so be sure to bookmark my page!</h1>");
      }
    
      dt.setMonth( dt.getMonth() + 12 );
     
      document.cookie = 'counter=' + (now.getMonth() + 1) +'/' + now.getDate() + '/' + now.getFullYear()+'||'+ visits + ";expires=" + dt.toGMTString();
    }
    
    /* ]]> */
    </script>
    </head>
    <body onload="hitMySite()">
    </body>
    </html>
    Yeah that fixed the date, but the visitor hits displays as NaN times. Once again though Internet Explorer has no problem running the script as it works like it should. Not sure what is up with Mozilla...

  • #12
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Neewb View Post
    Yeah that fixed the date, but the visitor hits displays as NaN times. Once again though Internet Explorer has no problem running the script as it works like it should. Not sure what is up with Mozilla...
    In Firefox, try deleting all the cookies for the domain:

    Tools/Options/Privacy [Use custom settings] [Show Cookies]

    If that still doesn't work then maybe host your page somewhere.

  • #13
    New Coder
    Join Date
    Apr 2012
    Posts
    27
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    In Firefox, try deleting all the cookies for the domain:

    Tools/Options/Privacy [Use custom settings] [Show Cookies]

    If that still doesn't work then maybe host your page somewhere.
    Oh i did that already, last night in fact. What I noticed was that the original script that I posted for the thread actually works now, but has some strange garbage writing on the first line it looks like thisYou last visited this page on: 5/1/2012; visit=Tue%20May%2001%202012%2000%3A49%3A38%20GMT-0700%20%28Pacific%20Daylight%20Time%29
    You have been to this page 26 times. It does increment correctly, but with the extra garbage on the end. The other script that you typed up the date is correct, works in Explorer, but will not increment the visits in Mozilla. It displays as NaN times. Thank you for replying.


  •  

    Posting Permissions

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