如何确认使用Javascript打开浏览器窗口? [英] How do I confirm a browser window is open, with Javascript?
问题描述
我正在为某些成员从我的站点打开一个新的浏览器窗口.但是,有些人可能稍后会关闭它,或者它最初可能无法打开.
I'm opening a new browser window from my site for some of the members. However, some may later close it, or it might have initially failed to open.
是否存在可以在每个页面上运行的相当简单的Javascript片段,以确认是否打开了另一个浏览器窗口,如果没有,则提供了重新打开它的链接?
Is there a snippet of fairly plain Javascript that can be run on each page to confirm if another browser window is open, and if not, to provide a link to re-open it?
[说明:] 要检查的窗口打开的代码将在其他页面上运行-不仅在打开该窗口的同一窗口和URL中.想象一个用户登录,打开窗口(尝试打开),然后在关闭第二个窗口(或从不打开)之前,他们在同一选项卡/窗口(或其他窗口)中浏览了一段时间-我希望注意到在初次尝试打开/关闭窗口后,窗口已经关闭了一段时间,所以我不确定是否容易使用window.open()(带有popup_window_handle.closed)检查javascript的返回,或者确实有可能
[clarification:] The code to check is a window is open would be run on other pages - not just in the same window and URL that opened it. Imagine a user logging in, the window (tries to) open, and then they surf around in the same tab/window (or others) for some time before they close the 2nd window (or it never opened) - I want to be able to notice the window has been closed some time after the initial attempt at opening/after it's closed, so I'm not sure that checking the javascript's return from window.open() (with popup_window_handle.closed) is easily used, or indeed possible.
推荐答案
此出色,全面的文章 (几乎完全控制弹出窗口")应该回答有关javascript弹出窗口的所有问题.
This excellent, comprehensive article ("Almost complete control of pop-up windows") should answer all your questions about javascript popup windows.
" JavaScript 1.1还引入了窗口 closed 属性.使用此属性,可以检测窗口是否已打开并随后关闭.我们可以使用它来加载页面如果仍对启用JavaScript 1.1的浏览器仍然打开,则直接进入打开器窗口:"
"JavaScript 1.1 also introduced the window closed property. Using this property, it is possible to detect if a window has been opened and subsequently closed. We can use this to load a page directly into the opener window if it still open for JavaScript 1.1 enabled browsers:"
<script language="JavaScript"><!--
function open_main(page) {
window_handle = window.open(page,'main');
return false;
}
//--></script>
<script language="JavaScript1.1"><!--
function open_main(page) {
if (opener && !opener.closed) {
opener.location.href = page;
}
else {
window_handle = window.open(page,'main');
}
return false;
}
//--></script>
<a href="example.htm" onClick="return open_main('example.htm')">example.htm</a>
添加: 您可以通过以下方式引用弹出窗口的名称来在另一个页面中返回窗口句柄:
Addition: You can get the window handle back in another page by referring to the popup's name this way:
window_handle = window.open(page,'myPopupName');
我想在您的情况下,您应该考虑一种一致的方法来创建整个应用程序中所有弹出窗口的名称.
I guess in your case you should think about a consistent way to create all the names of the popup windows throughout the entire application.
这篇关于如何确认使用Javascript打开浏览器窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!