GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialrepublic.com — Set and Get Cookies in JavaScript - Tutorial Republic JAVASCRIPT BASIC
JS Introduction
JS Getting Started
JS Syntax
JS Variables
JS Generating Output
JS Data Types
JS Operators
JS Events
JS Strings
JS Numbers
JS If…Else
JS Switch…Case
JS Arrays
JS Sorting Arrays
JS Loops
JS Functions
JS Objects
JAVASCRIPT & DOM
JS DOM Nodes
JS DOM Selectors
JS DOM Styling
JS DOM Get Set Attributes
JS DOM Manipulation
JS DOM Navigation
JAVASCRIPT & BOM
JAVASCRIPT ADVANCED
JS Date and Time
JS Math Operations
JS Type Conversions
JS Event Listeners
JS Event Propagation
JS Borrowing Methods
JS Hoisting Behavior
JS Closures
JS Strict Mode
JS JSON Parsing
JS Error Handling
JS Regular Expressions
JS Form Validation
JS Cookies
JS AJAX Requests
JS ES6 Features
JAVASCRIPT EXAMPLES
JAVASCRIPT REFERENCE
Advertisements
JavaScript CookiesIn this tutorial you will learn how to create, read, update and delete a cookie in JavaScript. What is a CookieA cookie is a small text file that lets you store a small amount of data (nearly 4KB) on the user's computer. They are typically used for keeping track of information such as user preferences that the site can retrieve to personalize the page when user visits the website next time. Cookies are an old client-side storage mechanism that was originally designed for use by server-side scripting languages such as PHP, ASP, etc. However, cookies can also be created, accessed, and modified directly using JavaScript, but the process is little bit complicated and messy. Tip: A cookie can be up to 4 KB, including its name and values, cookies that exceed this length are trimmed to fit. Also, each time the browser requests a page to the server, all the data in the cookie is automatically sent to the server within the request. Warning: Don't store sensitive data such as a password or credit card information in cookies since it could potentially be manipulated by the malicious user. Creating a Cookie in JavaScriptIn JavaScript, you can create, read, and delete cookies with the To create or store a new cookie, assign a
document.cookie = "firstName=Christopher";
A cookie value cannot contain semicolons, commas, or spaces. For this reason, you will need to use the JavaScript's built-in function
document.cookie = "name=" + encodeURIComponent("Christopher Columbus");
By default, the lifetime of a cookie is the current browser session, which means it is lost when the user exits the browser. For a cookie to persist beyond the current browser session, you will need to specify its lifetime (in seconds) with a
document.cookie = "firstName=Christopher; max-age=" + 30*24*60*60;
You can also specify the lifetime of a cookie with the
document.cookie = "firstName=Christopher; expires=Thu, 31 Dec 2099 23:59:59 GMT";
Here's a function that sets a cookie with an optional ExampleTry this code »
By default, a cookie is available to all web pages in the same directory or any subdirectories of that directory. However, if you specify a
document.cookie = "firstName=Christopher; path=/";
Further, you can use the If a cookie created by a page on
document.cookie = "firstName=Christopher; path=/; domain=example.com";
There is also a boolean attribute named
document.cookie = "firstName=Christopher; path=/; domain=example.com; secure";
Reading a CookieReading a cookie is a slightly more complex because the In order to get the individual cookie from this list, you need to make use of ExampleTry this code »
Now we're going to create one more function ExampleTry this code »
Updating a CookieThe only way to update or modify a cookie is to create another cookie with the same ExampleTry this code »
Deleting a CookieTo delete a cookie, just set it once again using the same ExampleTry this code »
However, to delete a cookie using the
document.cookie = "firstName=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
Advertisements
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |