Javacript advanced Programming-data storage

Source: Internet
Author: User

1. Data Storage 1.1 Cookies

An HTTP cookie, a cookie that was originally used to store the reply message on the client.

(1). Restrictions, different browsers have a limited number of cookies under a specific domain name, it is best not to have 20 cookies under each domain name

(2). The composition of the cookie

    1. Name: A unique name that determines the cookie, is case-insensitive, and the Cookiede name must be URL-encoded
    2. Value: The string value stored in the cookie, the value must be URL encoded
    3. Domain: A cookie is valid for that domain, and all requests sent to that domain contain the cookie, which can contain subdomains, which are not explicitly set, then the domain is considered to be the domain from which the cookie is set.
    4. Path: A cookie should be sent to the server for that path of the specified domain
    5. Expiry time: timestamp indicating when the cookie should be deleted. By default, cookies are deleted when the browser ends the session, and if the previous time is set, the cookie is immediately deleted
    6. Security flag: After enactment, cookies are sent to the server only when the SSL connection is used.

(3). Gets or sets the cookie

When you get the cookie attribute, use Document.cookie to return all the cookie strings available for the current page (based on domain, path, expiration time, and security settings):

Name1=value1;name2=value2

All names and values are URL-encoded, so you must decode them to use the

When setting cookie properties, use the

Document.cookie= "name1=value1;name2=value2;domain=.wrox.com;path=/"

methods, names and values are best coded with URLs

1.2 Web Storage mechanism 1.2.1 Storage

The storage type stores data based on key-value pairs, in the following ways:

(1). Clear (): Delete all values

(2). GetItem (name): Gets the corresponding value according to the name specified

(3). Key (Index): Gets the value at the specified position according to index

(4). RemoveItem (name): Delete the key value pair for the specified name

(5). SetItem (Name,value): sets a corresponding value for the specified name

1.2.2 Sessionstorage

The Sessionstorage object stores data that is specific to a reply, which means that the data is lost when the browser is closed. Data can be manipulated using storage related methods

1.2.3 Globalstorage

Globalstorage to save data across a conversation, there are specific access restrictions, to use Globalstorage, you first specify which domains can access the data, and you can do so by using properties in square brackets.

globalstorage["Wrox.com"]

Each Property object of the Globalstorage is an storage instance that can manipulate the data using storage related methods

1.2.4 Localstorage

The Localstorage object replaces globalstorage as a scheme for persisting client data in the revised HTML5 specification, unlike Globalstorage, which cannot specify any access rules to access a Localstorage object. The page must be from the same domain name (invalid subdomain), using the same protocol, on the same port. Equivalent to Globalstorage[location.host]

Localstorage is an example of storage, and you can manipulate the data using storage related methods.

1.2.5 Storage Events

Any modifications to the storage object will trigger the storage event on the document, which has the following properties for the event object:

(1). Domain: Changed storage domain name

(2). Key: Set or delete the key name

(3). NewValue: If the value is set, the new value, or null if the value is deleted

OldValue: The value before the key is changed

Javacript advanced Programming-data storage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.