/    Sign up×
Community /Pin to ProfileBookmark

JavaScript cookies

[CODE]<html>

<head>
</head>

<body>

<script>

function WriteCookie()
{
if( document.myform.customer.value == “” )
{
alert (“Enter some value!”);
return;
}

cookievalue= escape(document.myform.customer.value) + “;”;
document.cookie=”name=” + cookievalue;
document.write (“Setting Cookies : ” + “name=” + cookievalue );
}

function ReadCookie()
{
var allcookies = document.cookie;
document.write (“All Cookies : ” + allcookies );

// Get all the cookies pairs in an array
cookiearray = allcookies.split(‘;’);
// Now take key value pair out of this array
for(var i=0; i<cookiearray.length; i++)
{
name = cookiearray[i].split(‘=’)[0];
value = cookiearray[i].split(‘=’)[1];
document.write (cookievalue);
}

}

</script>

<form name=”myform” action=””>
Enter name: <input type=”text” name=”customer”/>
<input type=”button” value=”Set Cookie” onclick=”WriteCookie();”/>
</form>

<form name=”back” action=””>
<p> click the following button and see the result:</p>
<input type=”button” value=”Get Cookie” onclick=”ReadCookie()”/>
</form>

</body>
</html>[/CODE]

I was reading [url]http://www.tutorialspoint.com/javascript/javascript_tutorial.pdf[/url] 93 page.

Its about cookies data writing/reading.
I tried to combine 2 codes from examples.

The problem is:
If I write something in empty field and click [ENTER], then
click on ‘get cookie’ button… I see no cookies, while according idea I should.

Maybe I understood something wrong or skipped?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rootFeb 18.2017 — Couple of things about cookies...

  • 1. theyre dead since HTML5's localStorage feature has arrived

  • 2. cookies are not private and only store 4kB of information compared to local storage of 5MB per domain

  • 3. people who are aware of cookie use have tools to block cookies, often clear cookies and do so in the name of privacy.

    Best option is to use localStorage option and cookies as a fallback.
  • ×

    Success!

    Help @bobjohn spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...