简体   繁体   English

在html页面之间传递javascript对象

[英]Pass javascript objects between html pages

I have made a class in javascript with an object that contains both variables and functions. 我在javascript中创建了一个包含变量和函数的对象类。 I instantiate the object within one .html page, but when I change to another .html page, the object seems to be 'empty'. 我在一个.html页面中实例化了该对象,但是当我切换到另一个.html页面时,该对象似乎是“空”的。 How can I pass the object between one page to another? 如何在一页之间传递对象到另一页?

You can have a look at Web Storage . 您可以看一下Web存储 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. 这将不允许您存储完整的JavaScript对象,但是您将可以存储任意数量的字符串名称/值对。 Either permanently (between browser restarts) or as part of a session. 永久(在浏览器重启之间)或作为会话的一部分。

Use localStorage, check this lib: https://github.com/marcuswestin/store.js 使用localStorage,检查此库: 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 派生“类似于jQuery”的语法: http//pastebin.com/x3KpKyr1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM