Pass javascript objects between html pages

2019-08-30 18:41发布

I have made a class in javascript with an object that contains both variables and functions. I instantiate the object within one .html page, but when I change to another .html page, the object seems to be 'empty'. How can I pass the object between one page to another?

2条回答
Animai°情兽
2楼-- · 2019-08-30 18:49

You can have a look at Web Storage. This won't allow you to store a full JavaScript object, however you will be able to store as many string name-value pairs as you want. Either permanently (between browser restarts) or as part of a session.

查看更多
叼着烟拽天下
3楼-- · 2019-08-30 18:49

Use localStorage, check this lib: https://github.com/marcuswestin/store.js

You can do something like:

store.set('foo', originalObject);

and then...

var restoredObject = store.get('foo');

Removing stored objects:

store.remove('foo');

Fork "jQuery-like" syntax: http://pastebin.com/x3KpKyr1

查看更多
登录 后发表回答