将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:
- 饼干.将值写入Cookie并在打开的窗口中读取.
- 作为URL哈希的一部分传递:window.open(url +'#'+ encodeURIComponent(JSON.stringify(json));
-
我会尝试
- Cookies. Write values to cookies and read it in the opened window.
- Pass as a part of url hash: window.open(url + '#' + encodeURIComponent(JSON.stringify(json));
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.