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

  • SEARCH
  • Home
  • 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

Why aren't cookies able to be referenced from a servlet filter? It just seems
My apps preferences aren't getting saved when the app gets killed or phone gets
I have a very simple C# question: aren't the following statements equal when dealing
We have a lot of unit tests but they aren't run every night. I
Aren't these equivalent? ==SCRIPT A== if (file_exists($file) == false) { return false; } ==SCRIPT
We aren't allowed to run applications in phone 7 in background.. So I thought
Possible Duplicate: Equal strings aren't equal (==) in Java? From Eclipse's Display window: messages.get(i).getMsg()
Aren't Javascript blocks supposed to execute in the order that they are placed on
Why aren't many commercial, 3D video games (not random open source 2D ones) written
Why aren't my results being filtered by the members from my [Group Hierarchy] returned

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.