0

So i am learning Javascript and trying to set and retrieve a cookie, my code all looks to be ok but there is obviously a problem here.

function init()
{
    var panel = document.getElementById("panel");

    var user = escape("Dan, 000456");
    var expiry = new Date();
    expiry.setTime(expiry.getTime() + (7*24*60*1000) );
    document.cookie = "myData=" + user + ";" + "expires=" + expiry.toGMTString() + ";";

    if (document.cookie)
    {
        var cookieString = unescape(document.cookie);
        var list = cookieString.split("=");
        if (list[0] === "myData")
        {
            var data = list[1].split(",");
            var userName = data[0];
            var userAcct = data[1];
        }
    }

    panel.innerHTML += "Cookie String:" + cookieString;
    panel.innerHTML += "<br>Split List:" + list;
    panel.innerHTML += "<br>User Name:" + userName;
    panel.innerHTML += "<br>User Account:" + userAcct;
}
document.addEventListener("DOMContentLoaded",init, false);

When I look in the results they are not what I am expecting to see:

Cookie String:undefined
Split List:undefined
User Name:undefined
User Account:undefined
4
  • 1
    you have a typo - getElementById is wrong in your code. Commented Dec 31, 2014 at 13:25
  • 2
    You have an extra "I" in getElementById() in line 3... Hence the "3" in the error message. Commented Dec 31, 2014 at 13:26
  • 1
    You have a typo on the third line: document.getElementByIId("panel"); Two I's "IId". Commented Dec 31, 2014 at 13:26
  • @JRulle - bam that shows the script now...HOWEVER! There is now an issue where by the results are all showing undefined.....Despite that I have defined them above :S Commented Dec 31, 2014 at 13:27

1 Answer 1

1

Your main issue is now that you have corrected your syntax errors is that the following line:

var user = escape("Dan, 000456");

note: I believe the escape function is now deprecated?

change your javascript to this and make sure your browser allows cookies:

    function init(){

    var panel = document.getElementById("panel");

    var user = ["Dan, 000456"];  //<--change #1
    var expiry = new Date();
    expiry.setTime(expiry.getTime() + (7*24*60*1000) );

    //change #2 below added the array index of user to set the cookie value for myData
    document.cookie = "myData=" + user[0] + ";" + "expires=" + expiry.toGMTString();



    if (document.cookie)
    {
        var cookieString = unescape(document.cookie);
        var list = cookieString.split("=");
        if (list[0] === "myData")
        {
            var data = list[1].split(",");
            var userName = data[0];
            var userAcct = data[1];
        }
    }

    panel.innerHTML += "Cookie String:" + cookieString;
    panel.innerHTML += "<br>Split List:" + list;
    panel.innerHTML += "<br>User Name:" + userName;
    panel.innerHTML += "<br>User Account:" + userAcct;
           }
document.addEventListener("DOMContentLoaded",init, false);

Also make sure your html looks like this:

<div id="panel">Test</div>

You can remove the Test from the div in the html above if you want. The Test value should be replaced by values in your panel.innerHTML assignments.

Sign up to request clarification or add additional context in comments.

2 Comments

So I have updated the code you have kindly provided and I can see why you have changed what you did. However, I am still getting undefined as mentioned above...?
Did you figure out the issues with your undefined error? I was also curious why you needed the document.addEventListener at the the bottom of your page? If you just call the function at the beginning of your script and your script is loaded loaded right before your closing body tag then it will work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.