JavaScript determines whether an object is an empty object, Localstorage and Sessionstorage differences

Source: Internet
Author: User
Tags sessionstorage

Determines whether an object is an empty object:

var obj ={};

1.

if (json.stringify (newobj) = = "{}") {
Console.log (' Kongduixiang ')
}else{
Console.log (' hehe ')
}

2.
if (Object.keys (newobj). length = = 0) {
Console.log (' Kongduixiang ');
}

Localstorage and Sessionstorage differences:

  • Localstorage and Sessionstorage are the same objects that are used to store client temporary information.

  • They can only store objects of the string type (although other primitive types of objects can be stored in the specification, but so far no browser has implemented them).

  • The Localstorage life cycle is permanent, which means that the information will always be present unless the user is shown to clear Localstorage information on the UI provided by the browser.

    The Sessionstorage life cycle is the current window or tab, and once the window or tab is permanently closed, all data stored through sessionstorage is emptied.

  • The information in Localstorage or sessionstorage cannot be shared by different browsers. The same localstorage can be shared between different pages in the same browser (the page belongs to the same domain name and port), but sessionstorage information cannot be shared between pages or tabs. It is important to note that pages and tabs refer only to top-level windows, and if a tab contains multiple IFRAME tags and they belong to the same-origin page, they can be shared sessionstorage.

  • Homologous Judgment rules: (Protocol, hostname, port number)

    Http://www.test.com

    Https://www.test.com (different sources, because the protocol is different)

    Http://my.test.com (different source, because host name is different)

    http://www.test.com:8080 (different sources, because the ports are different)

  • Localstorage and Sessionstorage Use the same API when using:

    Localstorage.setitem ("Key", "value");//Store a value "value" for the name "key"

    Localstorage.getitem ("key");//Get a value named "Key"

    To enumerate Localstorage methods:

    for (Var i=0;i<localstorage.length;i++) {

    var name = Localstorage.key (i)?;

    var value = Localstorage.getitem (name);?

    }

    To delete a method that stores information in Localstorage:

    Localstorage.removeitem ("key");//delete the information named "Key".

    Localstorage.clear ();? Empty all information in Localstorage

  • The information obtained through GetItem or directly using localstorage["key" is a copy of the actual store.

    For example:

    Localstorage.key = {value1: "value1"}?;

    Localstorage.key.value1= ' a '?;

    There is no way to do this. The following writing does not work for the actual stored value:

    ? Localstorage.getitem ("Key"). Value1= "a";

JavaScript determines whether an object is an empty object, Localstorage and Sessionstorage differences

Related Article

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.