Search code examples
javascripthtmlcookies

Javascript - How to display only the value of a cookie?


I have searched several places trying to find out how to do display only the value of a cookie rather than the whole key but they all seemed needlessly complex for what I'm doing. I've got a single cookie with only one key, userName = something, and I can't figure out how to display only the "something" rather than userName = something.

function userCookie(form)
{
    if(form.User_Name.value == "")
    {
        alert("Cannot accept a blank user name, please enter a valid name");
        return false;
    }
    else
    {
        document.cookie="userName=" + form.User_Name.value;
        alert(document.cookie);
        return false;
    }                       
}

function newWindow() 
{       
    var userWindow = window.open("","MyUserName","height=300,width=300");
    userWindow.document.open();
    userWindow.document.write("<p>Welcome Back</p>");
    userWindow.document.write(document.cookie);
    userWindow.document.close();
}

Solution

  • If you always have exactly one cookie then document.cookie.split("=")[1] would be simple enough.