How to send javascript object from one page to ano

2020-07-27 05:54发布

问题:

Assume i have javascript object called "call" and i have stored some values in it and i want to access the same object "call" not values. This type of approach is it possible?

If yes plese explain with simple example.

I know there are several ways we can send the object properties values from one page to another page by using post url, header, cookies so on but my requirment to access the whole object instanse form one page to another page.

Anything in this regard highly appreciated. Thanks in advance.

回答1:

Just in addition to @gulshanm01's answer to store and retrieve an object you can use JSON.parse() and JSON.stringify() to parse the stored object.

E.g:

var obj = {
    fruit: "banana",
    fruit2: "apple",
    fruit3: "orange"
};
//Store
localStorage.setItem("obj", JSON.stringify(obj));
//Then retrieve
var localObj = JSON.parse(localStorage.getItem(obj));
alert(localObj.fruit);


回答2:

you can use localStorage.setItem(key,value) and fetch the value on another page using localStorage.getItem(key)