Javascript父窗口如何将数据发送到弹出窗口? [英] How can a Javascript parent window send data to popup window?

查看:81
本文介绍了Javascript父窗口如何将数据发送到弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用HTML和Javascript编写基于浏览器的应用程序(或者说,对应用程序进行快速原型制作).我希望主窗口能够显示带有动态数据的弹出窗口.但是,我不知道如何在Javascript中将数据从父窗口推送到弹出窗口.注意,我假设应用程序可以在脱机"场景中使用,因此所有动态数据都应来自主窗口.

I have been writing a browser based application (or rather, rapid prototyping an application) using HTML and Javascript. I would like the main window to be able to display popup windows with dynamic data. However, I cannot figure out how to push data from a parent window to a popup window in Javascript. Note, I am working with the assumption that the application may be used in "offline" scenarios, so all dynamic data should be coming from the main window.

理想情况下,我想写

var popup = window.open("popup.html", someidentifier, "");
popup.document.getElementById("SomeIdInPopupHtml").innerHTML = "1,2,3,4";

但是,getElementById函数返回NULL.如何从父窗口将数据推送到弹出窗口?

However, the getElementById function returns NULL. How can I push data to popup windows from a parent window?

推荐答案

弹出窗口提供的内容与父域不在同一个域中吗?如果是这样,简短的答案就是你做不到.

Is the popup serving content from a different domain than the parent? If so, the short answer is you can't.

长答案是您可以发送弹出窗口的href片段(即,协议://server/path?query#fragment中#后面的部分).如果弹出窗口中的内容知道要检查其片段是否有更改,则可以将数据传递给它.

The long answer is that you can sent the popup's href fragment (i.e. the part after the # in protocol://server/path?query#fragment). If the content in the popup knows to check its fragment for changes, then you can pass data to it.

如果它来自同一域,那么只要存在具有该ID的元素,您的代码就应该可以工作.

If it's from the same domain then your code should work, as long as an element with that id exists.

这篇关于Javascript父窗口如何将数据发送到弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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