How to send javascript object from one page to another page?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 23:22:36

问题


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)



来源:https://stackoverflow.com/questions/21474044/how-to-send-javascript-object-from-one-page-to-another-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!