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 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    UK
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Hello, i remember you!

    Hello,

    I am tring to use a cookie, the idea is, when some one types into a textfield:

    "my name is xxxxx", "hello xxxxxx" is returned, and when the window is closed,
    and opened "hello again, xxxxx" is returned, I have made:


    Code:
    <html>
    <head>
    
    
    <script type = "text/javascript">
    
    var user_name;
    var expires = new Date();
    
    
    if ((document.cookie == "") == false) {
    
    var length = document.cookie.length - 1;
    var message = document.cookie.substr(5,length);
    
    document.write("<h2><center>Welcome back, " +document.cookie + "!</center></h2>");
    
    }
    
    function check() {
    
    user_name = document.getElementById("name").value;
    expires.setFullYear(expires.getFullYear() + 1);
    
    document.cookie = escape("name") + "=" + escape(user_name) + "; expires = " + expires.toGMTString();
    
    alert(document.cookie);
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    </script>
    </head>
    <body>
    
    
    
    
    
    Name: <input type = "text" id = "name" />
    
    <input type = "button" value = "Enter" onclick = "check()" />
    
    
    
    
    </body>
    </html>


    I do not need the alert, that was part of the tutorial.



    Thank you!

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    Code:
    <html>
    <head>
    
    
    </head>
    <body>
    
    
    <script type = "text/javascript">
    
    var days = 1; // the number of days to remember the name
    
    
    if (cookie('name')) {
     document.write("<h2><center>Welcome back, " +cookie('name') + "!</center></h2>");
    }
    
    function check() {
     var nme = document.getElementById("name").value;
     if (nme){
      document.cookie='name='+nme+';expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';
     }
    }
    
    function cookie(nme){
     var re=new RegExp(nme+'[^;]+','i');
     if (document.cookie.match(re)){
      return document.cookie.match(re)[0].split("=")[1];
     }
     return null
    }
    
    </script>
    
    
    
    Name: <input type = "text" id = "name" />
    
    <input type = "button" value = "Enter" onclick = "check()" />
    
    
    
    
    </body>
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Vic's script is fine but here is my improved version - check for invalid names, avoid document.write(), hide input textbox when not required etc.

    Code:
    <html><body>
    
    <head>
    
    </head>
    
    <span id = "message"></span>
    <div id = "inp" style="display:block">
    Enter Your Name: <input type = "text" id = "name" onblur = "check()" />
    </div>
    
    <script type = "text/javascript">
    
    var days = 1; // the number of days to remember the name
    
    if (cookie('name')) {
    document.getElementById("message").innerHTML = "<h2><center>Welcome back, " +cookie('name') + "!</center></h2>"
    document.getElementById("inp").style.display="none";
    }
    
    function check() {
    var nme = document.getElementById("name").value;
    nme = nme.replace(/[^a-z\-\s\']/gi,"");  // strip invalid characters
    nme = nme.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    nme = nme.replace(/\s{2,}/g," ");  // replace multiple spaces with one space
    nme = nme.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});  // capitalise initial letter of each word
    if (nme.length > 1) {  // at least two valid characters left
    document.getElementById("message").innerHTML = "<h2><center>Welcome " + nme + "!</center></h2>"
    document.getElementById("inp").style.display="none";
    document.cookie='name='+nme+';expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';
    }
    }
    
    function cookie(nme){
    var re=new RegExp(nme+'[^;]+','i');
    if (document.cookie.match(re)){
    return document.cookie.match(re)[0].split("=")[1];
    }
    return null
    }
    
    </script>
    
    </body>
    </html>
    Last edited by Philip M; 07-23-2012 at 12:21 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.


  •  

    Posting Permissions

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