2011-04-06 56 views
3

我已经失去了几个小时试图找出如何解决一个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中。

EDIT(2)

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

在此先感谢。

卢卡斯。

+0

做你曾经能够找到一个解决方案? – 2012-08-21 22:51:22

回答

1

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

+0

感谢您的回答,freaktm。其实我也尝试过,并没有奏效。问题是,将这些变量放在我的父窗口中是没有意义的,因为我必须为每个字段创建一个属性(在父窗口中有一些字段可以访问相同的javascript函数 – LucasM 2011-04-06 09:43:45

0

win.contentWindow会给你访问新的窗口对象。

然后你可以使用:

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

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

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

+0

谢谢,Soubok。尝试你的解决方案,但它似乎并没有工作,我仍然有相同的问题访问弹出窗口内的变量。 – LucasM 2011-04-06 09:44:47

+0

是同一个域中的弹出窗口(URL)?窗口和窗口之间的跨域访问子窗口可能是个问题。 – 2011-04-06 09:50:37

+0

是的。我想知道是否这些属性丢失了,因为一旦执行搜索(通过servlet)URL更改(到servlet URL),但我们总是在讲同一个域。 – LucasM 2011-04-06 09:54:37

1

我建议tou使用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... 
+0

谢谢,dominicbri7!我按照你的建议做了,在将打开弹出窗口的函数中设置两个属性。但之后,我尝试在弹出框中访问它们: var proptext = window.getAttribute('targetid'); alert(proptext); 也使用this.getAttribute ...但没有任何反应。 – LucasM 2011-04-06 10:02:20

0

访问父窗口的一个变量/开瓶器窗口可以通过window.opener.yourVariableName

相关问题