如何从两个不同的页面引用弹出窗口?

时间:2022-05-18 21:10:35

I need to allow a user to click a link in "page-1.htm" and open a popup window. Then, when the user browses to "page-2.htm" in the main browser window I need to be able to reference the popup window.

我需要允许用户单击“page-1.htm”中的链接并打开一个弹出窗口。然后,当用户在主浏览器窗口中浏览“page-2.htm”时,我需要能够引用弹出窗口。

JavaScript in "page-1.htm"

“page-1.htm”中的JavaScript

var playerWin = window.open("player.htm", "playerWin", "width=300,height=130");
playerWin.play("song.mp3");  // play() is a function in player.htm

JavaScript in "page-2.htm"

“page-2.htm”中的JavaScript

playerWin.play("tune.mp3");

This code in page-2.htm generates an error "playerWin is not defined". That is understandable because no variable named playerWin has been defined on page-2.htm.

page-2.htm中的此代码生成错误“playerWin未定义”。这是可以理解的,因为在页面2.htm上没有定义名为playerWin的变量。

My question is: Am I able to reference the popup window from page-2.htm?

我的问题是:我能从第2.htm页引用弹出窗口吗?

5 个解决方案

#1


I just did a quick test even after you leave the opener page, the popup still have 'opener' object and you can access it. So either poll the opener and reset the reference, or add a timer after you leave the page to wait and then relink.

即使你离开了开启者页面,我刚做了一个快速测试,弹出窗口仍然有'开启者'对象,你可以访问它。因此,要么轮询开启者并重置引用,要么在离开页面等待然后重新链接后添加计时器。

1.htm

<script>
var w = window.open("p.htm", "playerWin", "width=300,height=130");
</script>
<a href="2.htm">2</a>

p.htm

<a href="javascript:opener.w=this;">re-link</a>

2.htm

<script>
    var w;
</script>
<a  href="javascript:alert(w);">check</a>

#2


The answer is no. Most browsers will completely isolate the window and document from one navigation to the next. There is no where you could place the reference to the popup which will survive when you navigate to page2.

答案是不。大多数浏览器会将窗口和文档从一个导航完全隔离到下一个导航。没有地方可以将引用放在弹出窗口中,当你导航到page2时它将继续存在。

#3


You need to put page-1.htm in an iframe (that takes up the full browser window)... then launch and communicate with the popup via the parent window of page-1.

您需要将page-1.htm放在iframe中(占用完整的浏览器窗口)...然后通过第1页的父窗口启动并与弹出窗口通信。

ie. (sorry for pseudo code)

即。 (抱歉伪代码)

user clicks on link in page-1
page-1 asks it's parent window to open a popup
popup opens

user navigates away (in the iframe)
user clicks a link in page-2
page-2 asks it's parent window to close it's popup

So long as everything is all on the same domain, and navigation stays within the iframe this should work

只要所有内容都在同一个域中,并且导航保留在iframe中,这应该可行

#4


Haven't seen that before... I'm going to assume you are doing this because you have to and don't have the option to organize your pages differently.

之前没有见过......我会假设你这样做是因为你必须并且没有选择以不同的方式组织你的页面。

Since you are using ASP.NET, just have page-2 set a session value. Then have your player page run an AJAX timer to check the session variable then perform the appropriate action since it has a handle on the popup window.

由于您使用的是ASP.NET,因此只需将page-2设置为会话值即可。然后让你的播放器页面运行一个AJAX计时器来检查会话变量然后执行相应的操作,因为它在弹出窗口上有一个句柄。

#5


The accepted answer didn't work for me in Firefox 12.0. I'm currently working on a project where the application must have persistent knowledge of which popups are open/closed while navigating from page to page or refreshing the current page. I'm using JavaScript cookies to store information on the client every time a popup opens or closes. Not perfect, but it's a good option if you don't need something that's 100% foolproof. It seems to me that trying to maintain JavaScript variables while reloading the page is always going to be much less reliable, and trying to store information about popups on the server makes no sense at all, so what else is left besides cookies?

在Firefox 12.0中,接受的答案对我不起作用。我目前正在开发一个项目,在该项目中,应用程序必须始终了解在页面之间导航或刷新当前页面时打开/关闭哪些弹出窗口。每次弹出窗口打开或关闭时,我都会使用JavaScript cookie在客户端上存储信息。不完美,但如果你不需要100%万无一失的东西,这是一个不错的选择。在我看来,在重新加载页面时尝试维护JavaScript变量总是不太可靠,并且尝试在服务器上存储弹出窗口的信息根本就没有意义,那么除了cookie之外还剩下什么呢?

#1


I just did a quick test even after you leave the opener page, the popup still have 'opener' object and you can access it. So either poll the opener and reset the reference, or add a timer after you leave the page to wait and then relink.

即使你离开了开启者页面,我刚做了一个快速测试,弹出窗口仍然有'开启者'对象,你可以访问它。因此,要么轮询开启者并重置引用,要么在离开页面等待然后重新链接后添加计时器。

1.htm

<script>
var w = window.open("p.htm", "playerWin", "width=300,height=130");
</script>
<a href="2.htm">2</a>

p.htm

<a href="javascript:opener.w=this;">re-link</a>

2.htm

<script>
    var w;
</script>
<a  href="javascript:alert(w);">check</a>

#2


The answer is no. Most browsers will completely isolate the window and document from one navigation to the next. There is no where you could place the reference to the popup which will survive when you navigate to page2.

答案是不。大多数浏览器会将窗口和文档从一个导航完全隔离到下一个导航。没有地方可以将引用放在弹出窗口中,当你导航到page2时它将继续存在。

#3


You need to put page-1.htm in an iframe (that takes up the full browser window)... then launch and communicate with the popup via the parent window of page-1.

您需要将page-1.htm放在iframe中(占用完整的浏览器窗口)...然后通过第1页的父窗口启动并与弹出窗口通信。

ie. (sorry for pseudo code)

即。 (抱歉伪代码)

user clicks on link in page-1
page-1 asks it's parent window to open a popup
popup opens

user navigates away (in the iframe)
user clicks a link in page-2
page-2 asks it's parent window to close it's popup

So long as everything is all on the same domain, and navigation stays within the iframe this should work

只要所有内容都在同一个域中,并且导航保留在iframe中,这应该可行

#4


Haven't seen that before... I'm going to assume you are doing this because you have to and don't have the option to organize your pages differently.

之前没有见过......我会假设你这样做是因为你必须并且没有选择以不同的方式组织你的页面。

Since you are using ASP.NET, just have page-2 set a session value. Then have your player page run an AJAX timer to check the session variable then perform the appropriate action since it has a handle on the popup window.

由于您使用的是ASP.NET,因此只需将page-2设置为会话值即可。然后让你的播放器页面运行一个AJAX计时器来检查会话变量然后执行相应的操作,因为它在弹出窗口上有一个句柄。

#5


The accepted answer didn't work for me in Firefox 12.0. I'm currently working on a project where the application must have persistent knowledge of which popups are open/closed while navigating from page to page or refreshing the current page. I'm using JavaScript cookies to store information on the client every time a popup opens or closes. Not perfect, but it's a good option if you don't need something that's 100% foolproof. It seems to me that trying to maintain JavaScript variables while reloading the page is always going to be much less reliable, and trying to store information about popups on the server makes no sense at all, so what else is left besides cookies?

在Firefox 12.0中,接受的答案对我不起作用。我目前正在开发一个项目,在该项目中,应用程序必须始终了解在页面之间导航或刷新当前页面时打开/关闭哪些弹出窗口。每次弹出窗口打开或关闭时,我都会使用JavaScript cookie在客户端上存储信息。不完美,但如果你不需要100%万无一失的东西,这是一个不错的选择。在我看来,在重新加载页面时尝试维护JavaScript变量总是不太可靠,并且尝试在服务器上存储弹出窗口的信息根本就没有意义,那么除了cookie之外还剩下什么呢?