简体   繁体   English

javascript:如何从window.opener访问window.top?

[英]javascript: how to access window.top from window.opener?

I am trying to refer to window.top in my popup window here http://www.globalrph.com/davescripts/popup.htm 我正在尝试在http://www.globalrph.com/davescripts/popup.htm的弹出窗口中引用window.top

once in the popup I want to access the main window's document but window.top is returning undefined. 一次在弹出窗口中,我想访问主窗口的文档,但是window.top返回未定义。

To access your popup's opener use. 要访问弹出窗口的打开器使用。

window.opener

To access your opener's top (eg your opener is in an iframe) 访问开瓶器的顶部(例如,开瓶器位于iframe中)

window.opener.top

Once at the opener window you want you can access the document to change its context, the location to manipulate the URL etc. 进入打开器窗口后,您希望可以访问文档以更改其上下文,操作URL的位置等。

If you want to move your popup to the same top position as the opening browser window and centered horizontally to the size of the opening browser window, in modern browsers you can use: 如果要将弹出窗口移动到与打开的浏览器窗口相同的顶部位置,并在水平方向上居中于打开的浏览器窗口的大小,请在现代浏览器中使用:

window.moveTo(window.opener.screenX + (window.opener.outerWidth / 2) - (window.outerWidth / 2), window.opener.screenY)

outerWidth and screenX and screenY work in IE9 and above and in modern browsers. externalWidth和screenX和screenY在IE9及更高版本和现代浏览器中均可使用。

moveTo has been around a long time. moveTo已经存在了很长时间。

In this case we're getting the left position of the opening browser window, calculating its center and subtracting half the width of the popup. 在这种情况下,我们将获得打开的浏览器窗口的左侧位置,计算其中心并减去弹出窗口宽度的一半。 That gives the center position. 那给出了中心位置。 Then we use window.opener.screenY to get the top position of the opening browser window and setting the popup's top the same. 然后,我们使用window.opener.screenY获取打开的浏览器窗口的顶部位置,并将弹出窗口的顶部设置为相同。

Many browsers have popup blockers turned on by default and will require the user to click a link to open a popup. 许多浏览器默认情况下都启用了弹出窗口阻止程序,并且需要用户单击链接才能打开弹出窗口。 I have this web application that opens a popup window to preview the results. 我有这个Web应用程序,它打开一个弹出窗口来预览结果。 A lot of web applications use a positioned div for dialogs. 许多Web应用程序将定位的div用于对话框。 Not knowing your application, I really can't give advice as to the best approach to take. 不知道您的应用程序,我真的无法就最佳方法提出建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM