如何从弹出窗口访问窗口变量
how to access a window variable from a popup window?
我已经浪费了几个小时试图找出如何解决一个javascript问题。我有一个JS函数,打开一个弹出窗口:
function openSearchWindow(searchType, targetIdField, targetDescField)
和我设置了两个属性到这个新窗口:
var win = window.open(searchPage, searchType + "search", style);
win.targetIdField = targetIdField;
win.targetDescField = targetDescField;
在此之前,一切都很完美。然而,在我的弹出窗口中,我需要访问我之前设置的两个变量:win。targetIdField和win.targetDescField
如何访问它们?我几乎什么都试过了。
编辑-及时,
在弹出窗口我有一个搜索引擎实际上。当用户单击结果时,有一个JS函数获取所选项目的ID和DESCRIPTION,并将其传递回"打开器"窗口,分别将它们放置在targetIdField和targetDescField中。
编辑(2)一旦在弹出窗口中执行搜索(通过servlet),弹出窗口的URL就会改变,但它总是在同一个域中。
提前感谢。
卢卡斯。
您是否尝试过通过window.targetIdField
访问它们?如果这不起作用,您可以在父窗口上绑定2个属性(使用window.targetIdField
而不是win.targetIdField
),然后使用window.opener.targetFieldid
从打开的窗口访问这些属性。
我建议使用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...
win.contentWindow
将允许您访问新的窗口对象。
那么你可以使用:
win.contentWindow.targetIdField = targetIdField;
win.contentWindow.targetDescField = targetDescField;
为新弹出的窗口对象设置targetIdField
和targetDescField
变量。
请注意,跨窗口访问仅限于同一域
从父窗口/打开器窗口访问变量可以通过window.opener.yourVariableName
相关文章:
- 从另一个窗口访问document.getElementById
- 从子弹出子窗口访问变量
- 如何从Chrome扩展程序的弹出窗口访问后台页面中的对象
- 如何防止 iframe 访问父窗口,但允许父窗口访问 iframe 文档
- 从父窗口访问 iframe 中的 DOM 附加函数
- 从子窗口访问父 URL 时出现问题
- 如何通过javascript从父窗口访问子窗口
- 如何从父窗口访问子窗口 URL
- 从 HTTPS 父窗口访问 iframe 中的 HTTP
- 使用Jquery从iframe内的子窗口访问父窗口滚动事件
- 从一个窗口访问其他选项卡
- 如何在弹出窗口刷新后从父窗口访问弹出DOM
- 从子窗口访问父窗口的角度作用域
- 从弹出窗口访问表行
- Web应用chrome, javascript,添加函数到子窗口,从父窗口访问它们
- 创建一个cookie,通过域B中的弹出窗口访问域a
- 从主窗口访问iframe内部属性
- jQuery多窗口访问和处理程序
- Javascript-从子窗口访问全局变量
- javascript, iframe, security - 从父窗口访问 js 函数时权限被拒绝