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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript Cookie Issue

    I am trying to write a script that stores and reads cookies that track the number of times a user has visited a Web site and the date of their last visit. The first time a user visits, a message should display welcoming them to the Web site and remind them to bookmark the page.

    I have the following but can't get the first time visit message to work. Any help would be greatly appreciated. Thanks in advance.


    Code:
    <!DOCTYPE html >
    <HEAD> 
    <SCRIPT LANGUAGE="JavaScript">
    
    var expDays = 365;
    var exp = new Date(); 
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
    
    function Who(info){
    var VisitorName = GetCookie('VisitorName')
    if (VisitorName == null){
    VisitorName = prompt("Please enter your name");
    SetCookie ('VisitorName', VisitorName, exp);
    }
    return VisitorName;
    }
    
    function When(info){
    var rightNow = new Date()
    var WWHTime = 0;
    WWHTime = GetCookie('WWhenH')
    WWHTime = WWHTime * 1
    var lastHereFormatting = new Date(WWHTime);
    var intLastVisit = (lastHereFormatting.getYear() * 10000)+(lastHereFormatting.getMonth() * 100) + lastHereFormatting.getDate()
    var lastHereInDateFormat = "" + lastHereFormatting;
    var dayOfWeek = lastHereInDateFormat.substring(0,3)
    var dateMonth = lastHereInDateFormat.substring(4,11)
    var timeOfDay = lastHereInDateFormat.substring(11,16)
    var year = lastHereInDateFormat.substring(23,25)
    var WWHText = dayOfWeek + ", " + dateMonth + " at " + timeOfDay
    SetCookie ("WWhenH", rightNow.getTime(), exp)
    return WWHText
    }
    
    function Count(info){
    var WWHCount = GetCookie('WWHCount')
    if (WWHCount == null) {
    WWHCount = 0;
    }
    else{
    WWHCount++;
    
    }
    SetCookie ('WWHCount', WWHCount, exp);
    return WWHCount;
    }
    
    function firstVisit(info) {
    var WWHCount = GetCookie('WWHCount')
    var counter = WWHCount
    window.alert(counter);
    if (counter == 0) {
    fistVisitMsg = ("This is your first visit to our site. Please Bookmark us for future use"); 
    }
    else {
    return;
    }
    }
    
    function set(){
    VisitorName = prompt("Please enter your name");
    SetCookie ('VisitorName', VisitorName, exp);
    SetCookie ('WWHCount', 0, exp);
    SetCookie ('WWhenH', 0, exp);
    }
    
    function getCookieVal (offset) {  
    var endstr = document.cookie.indexOf (";", offset);  
    if (endstr == -1)    
    endstr = document.cookie.length;  
    return unescape(document.cookie.substring(offset, endstr));
    }
    
    function GetCookie (name) {  
    var arg = name + "=";  
    var alen = arg.length;  
    var clen = document.cookie.length;  
    var i = 0;  
    while (i < clen) {    
    var j = i + alen;    
    if (document.cookie.substring(i, j) == arg)      
    return getCookieVal (j);    
    i = document.cookie.indexOf(" ", i) + 1;    
    if (i == 0) break;   
    }  
    return null;
    }
    
    function SetCookie (name, value) {  
    var argv = SetCookie.arguments;  
    var argc = SetCookie.arguments.length;  
    var expires = (argc > 2) ? argv[2] : null;  
    var path = (argc > 3) ? argv[3] : null;  
    var domain = (argc > 4) ? argv[4] : null;  
    var secure = (argc > 5) ? argv[5] : false;  
    document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) +  ((domain == null) ? "" : ("; domain=" + domain)) +    ((secure == true) ? "; secure" : "");
    }
    
    function DeleteCookie (name) {  
    var exp = new Date();  exp.setTime (exp.getTime() - 1);  
    var cval = GetCookie (name);  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    }
    
    </SCRIPT> 
    <BODY> 
    <CENTER>
    <SCRIPT LANGUAGE="JavaScript">
    document.write.fistVisitMsg
    document.write("<p>Hi " + Who() + ". You've been here " + Count() + " time(s).  Last time was " + When() +".");
    </SCRIPT>
    </CENTER>
    </body>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    This question was asked (and answered) quite recently. Have you tried using the search feature of the forum? It is a bit tedious posting the same answer multiple times.

    What error messages do you receive? Have you tried using your error console (press F12 key)? Or putting alerts in at strategic positions so you can inspect the values?

    You should bear in mind that users may (and often do) delete their cookies - sometimes once a day if they use CCleaner. Also, the dates and number of visits to your site (even if accurate) is visible only to the user himself - not to you.
    Last edited by Philip M; 04-08-2013 at 11:37 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Philip. I did do a search on the site but didn't find anything related. When I run the code as written, I receive no error messages. The code will run but I an not getting the "initial visit" message. I did place an alert in that section of the code but I don't get the alert either. It's like that section of the code is bypassed. The error console isn't showing anything either.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    document.write.fistVisitMsg
    should be
    document.write(fistVisitMsg)

  • #5
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by qcns1 View Post
    The code will run but I an not getting the "initial visit" message. I did place an alert in that section of the code but I don't get the alert either. It's like that section of the code is bypassed. The error console isn't showing anything either.
    Even when you fix the document.write error you still won't get the first visit message at all because your function firstVisit(info) isn't being called anywhere..!!!!

    Make sure your error console in your browser is turned on and your error messages will appear. They do in mine when I run your code.

  • #6
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still not seeing it. I have run this code in IE, Firefox and Chrome. All work with no errors that I can see and the FirstVisitMsg is not working.


  •  

    Posting Permissions

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