如何从弹出窗口中访问窗口变量?

时间:2011-04-06 09:18:35

标签: javascript popup

我已经失去了几个小时试图弄清楚如何解决javascript问题。我有一个JS函数打开一个弹出窗口:

function openSearchWindow(searchType, targetIdField, targetDescField)

我在这个新窗口中设置了2个属性:

var win = window.open(searchPage, searchType + "search", style);
win.targetIdField = targetIdField;
win.targetDescField = targetDescField;

直到这里,一切都很完美。但是,在我的弹出窗口中,我需要访问我之前设置的这两个变量:win.targetIdField和win.targetDescField

我如何访问它们?我几乎尝试了一切。

编辑 - 及时,

在弹出窗口中,我实际上有一个搜索引擎。当用户单击结果时,有一个JS函数可以获取所选项的ID和描述,并将其传递回“opener”窗口,分别将它们放在targetIdField和targetDescField中。

编辑(2)

一旦在弹出窗口中执行搜索(通过servlet),弹出窗口的URL就会改变,但它总是在同一个域内。

提前致谢。

卢卡斯。

4 个答案:

答案 0 :(得分:1)

您是否尝试通过window.targetIdField访问它们?如果thait不起作用,您可以在父窗口上绑定2个属性(使用window.targetIdField而不是win.targetIdField),然后使用window.opener.targetFieldid从打开的窗口访问这些属性。

答案 1 :(得分:1)

我建议使用setAttribute和getAttribute,因为每个浏览器都支持它(我知道)。

//Setting the properties (in the parent window)
win.setAttribute('targetIdField', targetIdField);
win.setAttribute('targetDescField', targetDescField);

//Accessing the properties (in the pop-up window)
window.getAttribute('targetIdField'); //you might need to use lowercase letters...
//If that doesn't work you can try multiple things
this.getAttribute...

答案 2 :(得分:0)

win.contentWindow将允许您访问新窗口对象。

然后你可以使用:

win.contentWindow.targetIdField = targetIdField;
win.contentWindow.targetDescField = targetDescField;

为新弹出窗口对象设置targetIdFieldtargetDescField变量。

请注意,跨窗口访问仅限于同一个域。

答案 3 :(得分:0)

从父窗口/开启窗口访问变量可以通过window.opener.yourVariableName

完成
相关问题