将json对象从一个HTML页面传递到另一HTML页面 [英] Pass json object from one HTML page to another

查看:335
本文介绍了将json对象从一个HTML页面传递到另一HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用window.open()打开一个页面.有没有一种方法可以将JSON对象从父页面传递到子页面?当然,可以将数据从父级写入cookie并从新页面中读取相同的cookie.还有更简单的方法吗?

I am opening a page using window.open(). Is there a way to pass a JSON object from the parent to the child page? Ofcourse writing data to a cookie from the parent and reading that same cookie from the new page might be an option. Any simpler ways?

推荐答案

我看到有几种方法可以做到这一点:

I see there are several ways to do this:

  1. 饼干.将值写入Cookie并在打开的窗口中读取.
  2. 作为URL哈希的一部分传递:window.open(url +'#'+ encodeURIComponent(JSON.stringify(json));
  3. 我会尝试

  1. Cookies. Write values to cookies and read it in the opened window.
  2. Pass as a part of url hash: window.open(url + '#' + encodeURIComponent(JSON.stringify(json));
  3. I would try to

winRef = window.open(...);

winRef = window.open(...);

winRef.postMessage(...);

winRef.postMessage(...);

https://developer.mozilla.org/en/DOM/window.postMessage

我没有尝试第三个选项,但是它可能是1和2的不错选择.

I didn't try the third option, but it might be a nice alternative to 1 and 2.

这篇关于将json对象从一个HTML页面传递到另一HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆