Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6610391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:50:06+00:00 2026-05-25T19:50:06+00:00

why aren’t the cookies saved when i run the following script: window.onload=init; function init()

  • 0

why aren’t the cookies saved when i run the following script:

window.onload=init;

function init() {
var userName="";
if(document.cookie != "") {
    username=document.cookie.split("=")[1];
}

document.getElementById("name_field").value = username;
document.getElementById("name_field").onblur = setCookie;
}

function setCookie() {
var exprDate = new Date();
exprDate.setMonth(exprDate.getMonth() + 6);

var userName = document.getElementById("name_field").value;
document.cookie = "username=" + username + ";path=/;expires=" + exprDate.toGMTString();
}

When i refresh the page the text-field gets empty? why is so ?
HTML

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="writing_cookie.js">
</script>
</head>

<body>
<form>
<label>Enter your name&nbsp;&nbsp;<input type="text" id="name_field" /></label> <br/>
<input type="submit" value="submit" />
</form>
</body> 
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:50:07+00:00Added an answer on May 25, 2026 at 7:50 pm

    As @pimvdb says, you mix the case of username.

    In javascript, things are case sensitive – userName is not the same variable as username so you need to be consitant with your casing.

    The specific problem is that you assign the value of the name_field to userName in setCookie() but on the next line you save username to the cookie. username is uninitialised so you save an empty value to the cookie. This empty value is what you get from the cookie in init()

    Also, in init() you declare userName but assign the value of the cookie to username. This doesn’t actually cause a problem but it could do with fixing all the same.

    Edit – I’ve just seen your reply to @pimvdb’s comment and done a bit more research:

    When reading the cookie, you need to split on “;”, not “=”. This will then give you an array of “username=username”, “path=path” and “expires=date”. You then would extract the username from the first element of the array by looking for the “=”

    Your code splits on “=” which would give an array of “username”, “username;path”, “/;expires” and “date“.

    I would have expected it to have put something in the text field with your code though (after you’d fixed the casing issue).

    (Reference page here)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a lot of unit tests but they aren't run every night. I
My apps preferences aren't getting saved when the app gets killed or phone gets
When you get a badge or aren't logged in to stack overflow there's a
A while ago I read the Mocks Aren't Stubs article by Martin Fowler and
The docs at www.fastcgi.com aren't clear on this (to me), so I hope someone
I've read (and re-read) Martin Fowler's Mocks Aren't Stubs . In it, he defines
I want to enumerate all available drive letters (which aren't already taken) in Windows
PHP treats all arrays as associative, so there aren't any built in functions. Can
So far I've found it impossible to produce usable tracebacks when Mako templates aren't
I always try to avoid to return string literals, because I fear they aren't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.