Javascript 如何将javascript对象从一个页面传递到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7709289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:17:04  来源:igfitidea点击:

How to pass javascript object from one page to other

javascriptjquery

提问by Dipen

I want to pass javascript object from one page to other page so anyone can tell me how to do it?

我想将 javascript 对象从一个页面传递到另一个页面,以便任何人都可以告诉我该怎么做?

Is that possible to do so using jQuery?

使用 jQuery 可以这样做吗?

回答by Robert Koritnik

Few ways

几种方式

  1. Server side postback

    Have a POST form on your page and save your serialized object inside a hidden input then post it to the other page. You will be able to process that data on the server and most likely put it back somehow into the page. Either as javascript object or anything else.

  2. Client side URL examination

    Make a GET request to your otherpage by attaching your serialized object to URL as:

    http://www.app.com/otherpage.xyz?MyObject=SerializedData

    That otherpage can then easily parse its URL and deserialize data using Javascript.

  3. What's in a window.name= local cross-page session

    This is a special technique that's also used in a special javascript librarythat exposes window.nameas a dictionary, so you can save many different objects into it and use it as local cross-page-session. It has some size limitations that may affect you, but check the linked page for that and test your browsers.

  4. HTML5 local storage

    HTML5 has the ability of local storage that you can use exactly for these purposes. But using it heavily depends on your browser requirements. Modern browsers support it though and data can be restored even after restarting browsers or computers...

  5. Cookies

    You can always use cookies but you may run into their limitations. These days cookies are not the best choice even though they have the ability to preserve data even longer than current window session.

  1. 服务器端回传

    在您的页面上有一个 POST 表单并将您的序列化对象保存在隐藏的输入中,然后将其发布到另一个页面。您将能够在服务器上处理该数据,并且很可能以某种方式将其放回页面中。无论是作为 javascript 对象还是其他任何东西。

  2. 客户端 URL 检查

    通过将序列化对象附加到 URL来向您的其他页面发出 GET 请求:

    http://www.app.com/otherpage.xyz?MyObject=SerializedData

    其他使用Javascript网页就可以轻松地分析它的URL和反序列化数据。

  3. 有什么意义window.name=本地跨页会话

    这是一种特殊的技术,也用于作为字典公开的特殊 javascript 库window.name,因此您可以将许多不同的对象保存到其中并将其用作本地跨页面会话。它有一些大小限制可能会影响您,但请检查链接页面并测试您的浏览器。

  4. HTML5 本地存储

    HTML5 具有本地存储功能,您可以将其完全用于这些目的。但是使用它很大程度上取决于您的浏览器要求。虽然现代浏览器支持它,即使在重新启动浏览器或计算机后也可以恢复数据......

  5. 饼干

    您可以随时使用 cookie,但您可能会遇到它们的限制。尽管 cookie 能够比当前窗口会话保存更长的数据,但如今 cookie 并不是最佳选择。

Javascript object serialization

Javascript 对象序列化

You will of course have to use some sort of a (de)serializer on your client side in some of the upper cases. Deserializers are rather easy to find (jQuery already includes a great function $.getJSON()) and are most likely part of your current javascript library already (not to even mention eval()).

在某些大写情况下,您当然必须在客户端使用某种(反)序列化程序。反序列化器很容易找到(jQuery 已经包含了一个很棒的功能$.getJSON())并且很可能已经是您当前 javascript 库的一部分(更不用说eval())。

But for object to JSON string serialization I'd recommend json2.js librarythat's also recommended by John Resig. This library uses in-browser implemented JSON (de)serialization features if they exist or uses it's own implementation when they don't. Hence recommendation.

但是对于 JSON 字符串序列化的对象,我推荐json2.js 库,该也由John Resig推荐。该库使用浏览器内实现的 JSON(反)序列化功能(如果它们存在)或在它们不存在时使用它自己的实现。因此推荐。

回答by Guffa

That is only possible if the pages exist at the same time, and one page is opened from the other so that you have a reference to the other pages windows object.

只有当页面同时存在时才有可能,并且一个页面是从另一个页面打开的,这样您就可以引用其他页面的 windows 对象。

If you navigate from one page to another, they don't exist at the same time, so you can't communicate like that. You would have to serialise the object into a string that you can send along in the request, for example sending JSON in the query string.

如果你从一个页面导航到另一个页面,它们不会同时存在,所以你不能那样交流。您必须将对象序列化为可以在请求中发送的字符串,例如在查询字符串中发送 JSON。

There are different ways of persisting data, like in the query string, post data, cookies, window name or HTML5 local storage, but all those methods can only persist string values, not Javascript objects.

持久化数据的方式有很多种,比如查询字符串、发布数据、cookies、窗口名称或 HTML5 本地存储,但所有这些方法只能持久化字符串值,而不是 Javascript 对象。

回答by Matt Sain

This is possible to do, and you have a couple of options.

这是可能的,您有几个选择。

Local Storage

本地存储

Can be added/eddited/removed at any stage and accessed across a domain. (doesn't work natively in ie6 and ie7 however there are work arounds for that)

可以在任何阶段添加/编辑/删除并跨域访问。(在 ie6 和 ie7 中本地不起作用,但有解决方法)

The Window Object

窗口对象

I would put a massive cavet around this not being the best solution, it's not at all secure, so only use it for things that don't need to be kept private. window.name = { "json" : "object"}which is then available in the following page in the window.name property.

我会在这不是最好的解决方案周围放一个很大的警告,它根本不安全,所以只将它用于不需要保密的事情。window.name = { "json" : "object"}然后在 window.name 属性中的下一页中可用。

回答by Greg Pettit

If your application uses sessions, you could serialize it (as per other answers) then POST it to the server where it is stored in a session variable. In the next page, you retrieve it from the session variable.

如果您的应用程序使用会话,您可以将其序列化(根据其他答案),然后将其 POST 到服务器,并将其存储在会话变量中。在下一页中,您从会话变量中检索它。

回答by freakish

I believe that the only way to pass a javascript object from one page to another is to serialize it into string and pass it in url. For example if you have object

我相信将 javascript 对象从一个页面传递到另一个页面的唯一方法是将其序列化为字符串并在 url 中传递它。例如,如果您有对象

var temp = { id: 1, value: 'test' }

you may want to use JSON-jsto serialize it and pass it in for example http://mysite.com/?data=serialization. Then after you load the page you need to deserialize it via for example $.parseJSON().

例如,您可能希望使用JSON-js 对其进行序列化并传入http://mysite.com/?data=serialization。然后在加载页面后,您需要通过例如反序列化它$.parseJSON()

回答by thomasrutter

In Javascript, normally all variables only exist in a scope that is unique to that page load. They don't persist between different pages if there is a new page load.

在 Javascript 中,通常所有变量仅存在于该页面加载所独有的范围内。如果有新页面加载,它们不会在不同页面之间持续存在。

The exceptions to this are

例外情况是

  1. Cookies.
  2. Local storage.
  1. 饼干。
  2. 本地存储。

Cookiesare truly cross-browser but are extremely limited in terms of size. You shouldn't expect to be able to store more than 4kB of cookies for a page reliably; in fact you probably shouldn't be using any more than 1kB. Cookie data slows down the loading of every page and other request, so it should be used sparingly.

Cookie是真正的跨浏览器,但在大小方面极为有限。您不应该期望能够为一个页面可靠地存储超过 4kB 的 cookie;事实上,您可能不应该使用超过 1kB 的空间。Cookie 数据会减慢每个页面和其他请求的加载速度,因此应谨慎使用。

There are various types of local storageavailable to Javascript, but the only practical cross-browser implementation of this is HTML5 webstoragewhich is implemented in all modern browsers (IE8+, FF, Chrome, Safari, etc), but is notably not implemented in IE6 or IE7, if that matters.

Javascript有多种类型的本地存储可用,但唯一实用的跨浏览器实现是HTML5 网络存储,它在所有现代浏览器(IE8+、FF、Chrome、Safari 等)中实现,但特别是在 IE6 中未实现或 IE7,如果这很重要。

Both these approaches store a value in the user's browser which can be made to be persistent so that it can be written to and read from by pages from the same site, even between page views (and even, often, between browser sessions or computer reboots).

这两种方法都在用户的浏览器中存储一个值,该值可以持久化,以便可以从同一站点的页面写入和读取它,甚至在页面视图之间(甚至通常在浏览器会话或计算机重新启动之间) )。

回答by vise

I wrote a library some time ago, that can store most js objects to localstorage. For example instances of your prototype classes, with references to other objects, self references included. Bare in mind that IE support is lackluster.

前段时间写了一个,可以将大部分js对象存储到localstorage。例如,原型类的实例,包含对其他对象的引用,包括自引用。请记住,IE 支持乏善可陈。