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 6129903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:47:47+00:00 2026-05-23T16:47:47+00:00

Using Javascript, I want to create a cookie with the value of an input

  • 0

Using Javascript, I want to create a cookie with the value of an input field with the id “username” everytime a button is pressed. I then want to read that cookie and display it on the website. Though I just tried this to test my knowledge of cookies, I would now like to know the solution to this problem, because my code does not work.

I am gonna be using these two functions from elated.com:

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
var cookie_string = name + "=" + escape ( value );

if ( exp_y )
{
   var expires = new Date ( exp_y, exp_m, exp_d );
   cookie_string += "; expires=" + expires.toGMTString();
}

if ( path )
    cookie_string += "; path=" + escape ( path );

if ( domain )
    cookie_string += "; domain=" + escape ( domain );

if ( secure )
    cookie_string += "; secure";

document.cookie = cookie_string;




function get_cookie ( cookie_name )
{
var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

 if ( results )
     return ( unescape ( results[2] ) );
else
    return null;
}

When the button is pressed this function is going to run:

function Register()
{
var username = document.getElementById("username").value; //Read the input value
set_cookie("username", username, 2012, 12, 23); //Set the cookie
document.write(get_cookie("username")); //Read the cookie
}

Whenever I try to run this, the get_cookie function returns null. This means that it’s at least being run successfully, but not finding any cookie named “username”, even though I just created it. Can anybody help?

  • 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-23T16:47:47+00:00Added an answer on May 23, 2026 at 4:47 pm

    You say you’re using Chrome. Chrome (and Chromium) don’t allow setting or accessing cookies on local pages, as a deliberate decision over security concerns (see http://code.google.com/p/chromium/issues/detail?id=535#c3). The code you posted does work if put onto a server and sent out, but won’t behave correctly if you open the page from your local system as a file:// URL. That’s also why the jsfiddle works.

    Some other browsers do allow cookies in local pages, so if you’re looking to experiment with JavaScript and cookies locally you might be better trying one of those. Firefox allows them with the expected behaviour, although they have considered changing it and might do in future. Chrome also has a command-line flag –enable-file-cookies that you could use while testing, but they don’t recommend it for everyday use.

    You could also put your page onto a server and test there. Either one of the free hosting services or an instance of Apache or similar on your local computer would work. The important part is just that the page is accessed over HTTP.

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

Sidebar

Related Questions

I want to create an iframe using javascript and then insert it into two
Let's say using Javascript, I want to match a string that ends with [abcde]*
I want to hide the Next button on my ASP.NET Wizard control using JavaScript.
I want to create inline SVG graphics using Javascript. However, it seems like createElementNS
I want to create a input textbox that does not require a <input type=text>
I want to create torrent files with a Firefox extension written using javascript. Torrent
So, I can create an input button with an image using <INPUT type="image" src="/images/Btn.PNG"
Possible Duplicate: Custom alert using Javascript I want to create a confirmation message-Do you
i want to create a dynamic generated form using javascript, everything works fine, until
I want to create a barchart using the gRaphael javascript library. Does gRaphael support

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.