1. Welcome to LilyPad. Download the project, explore the forums, and create your own LilyPad network.


    If you use the software and enjoy it or have a question, or would like to contribute to the future of the software directly or through resources, please sign up and join our little community.

A little help with JavaScript?

Discussion in 'Programming' started by 123isme1, Feb 27, 2014.

  1. 123isme1

    123isme1 New Member

    So, I made a thinggy that will, when activated ask for your name, then create a cookie, when the page is reloaded, it says "welcome back Name." the thing is, I want to make it so it will reload the page, and say it right after you type it in. I don't know what to put, and where to put it.
    Here is my code so far:

    Code (html5):
    <!DOCTYPE html>
    <html>
    <head><script>

    function setCookie(cname,cvalue,exdays)
    {
    var d = new Date();
    d.setTime(d.getTime()+(exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires;
    }

    function getCookie(cname)
    {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++)
     {
     var c = ca[i].trim();
     if (c.indexOf(name)==0) return c.substring(name.length,c.length);
     }
    return "";
    }

    function checkCookie()
    {
    var user=getCookie("username");
    if (user!="")
     {
    document.write("Welcome again " + user);
     }
    else
     {
     user = prompt("Please enter your name:","");
     if (user!="" && user!=null)
       {
       setCookie("username",user,30);
       }
     }
    }

    </script></head>

    <body onload="checkCookie()"></body>
    </html>
     
    Last edited by a moderator: Feb 27, 2014
  2. Coelho

    Coelho Software Engineer Staff Member Administrator Maintainer

    Code (javascript):

    location.reload();
     
    Will reload the page.
    Last edited by a moderator: Feb 27, 2014

Share This Page