Javascript 检查窗口是否已经打开 window.open
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12138236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if window is already open window.open
提问by Saghir A. Khatri
I have a html page. In the body of the page I am calling onload
event which calls javascript function to open a pop up window. here is the code:
我有一个 html 页面。在页面的正文中,我正在调用onload
调用 javascript 函数以打开弹出窗口的事件。这是代码:
var newWindow = null;
function launchApplication()
{
if ((newWindow == null) || (newWindow.closed))
{
newWindow = window.open('abc.html','','height=960px,width=940px');
}
}
when I move to another page, and come back to that page again, popup reopens, although it is already opened. Please guide me to proper direction so that if pop up is already open then it should not open again. I tried document.referred
but it requires the site online, currently I am working offline.
当我移动到另一个页面并再次返回该页面时,弹出窗口会重新打开,尽管它已经打开了。请指导我正确的方向,以便如果弹出窗口已经打开,则不应再次打开。我试过了,document.referred
但它需要网站在线,目前我正在离线工作。
采纳答案by Jon Hanna
newWindow = window.open('abc.html','com_MyDomain_myWindowForThisPurpose','height=960px,width=940px');
Give the window a name. Basing the name on your domain like this, prevents the chances of you picking a name someone else happened to choose.
为窗口命名。像这样将名称基于您的域,可以防止您选择其他人碰巧选择的名称。
Never make up a name that begins with _
, those are reserved for special names the browser treats differently (same as with the "target" attribute of anchor elements).
永远不要组成以 开头的名称_
,这些名称是为浏览器以不同方式处理的特殊名称保留的(与锚元素的“目标”属性相同)。
Note that if the window of that name was opened with different options (e.g. different height), then it'll keep those options. The options here will only take effect if there is no window of that name, so you do create a new one.
请注意,如果该名称的窗口使用不同的选项(例如不同的高度)打开,那么它将保留这些选项。此处的选项仅在没有该名称的窗口时才会生效,因此您需要创建一个新窗口。
Edit:
编辑:
Note that the "name" is of the window, not of the content. It doesn't affect the title (newWindow.document.title
will affect that, as of course will code in abc.html
). It does affect other attempts to do stuff across windows. Hence another window.open
with the same name will reuse this window. Also a link like <a href="def.html" target="com_MyDomain_myWindowForThisPurpose">clicky!</a>
will re-use it. Normal caveats about browsers resisting window-opening in various scenarios (popup-blocking) apply.
请注意,“名称”是窗口的,而不是内容的。它不会影响标题(newWindow.document.title
会影响它,当然会在 中编码abc.html
)。它确实会影响其他跨窗口执行操作的尝试。因此,另一个window.open
具有相同名称的窗口将重用此窗口。还有一个像这样的链接<a href="def.html" target="com_MyDomain_myWindowForThisPurpose">clicky!</a>
会重复使用它。关于浏览器在各种情况下拒绝打开窗口(弹出窗口阻止)的正常警告适用。
回答by Timo Huovinen
To open a window and keep a reference to it between page refresh.
打开一个窗口并在页面刷新之间保持对它的引用。
var winref = window.open('', 'MyWindowName', '', true);
if(winref.location.href === 'about:blank'){
winref.location.href = 'http://example.com';
}
or in function format
或以函数格式
function openOnce(url, target){
// open a blank "target" window
// or get the reference to the existing "target" window
var winref = window.open('', target, '', true);
// if the "target" window was just opened, change its url
if(winref.location.href === 'about:blank'){
winref.location.href = url;
}
return winref;
}
openOnce('http://example.com', 'MyWindowName');
回答by Andy Taw
You can check if the window is open or closed by re-assigning a reference to it when it closes. Example:
您可以通过在关闭时重新分配对窗口的引用来检查窗口是打开还是关闭。例子:
var newWindow;
var openWindow = function(){
newWindow = newWindow || window.open('newpage.html');
newWindow.focus();
newWindow.onbeforeunload = function(){
newWindow = null;
};
};
回答by A-312
When you move on another page (on the same domain), you can re-set the window.open variablewith popup page like this :
当您移动到另一个页面(在同一域上)时,您可以使用弹出页面重新设置window.open 变量,如下所示:
https://jsfiddle.net/u5w9v4gf/
https://jsfiddle.net/u5w9v4gf/
Step to try :
尝试步骤:
- Click on Run (on jsfiddle editor).
- Click on Try me (on preview).
- Click on Run to move on another page, the variable will be re-set.
- 单击运行(在 jsfiddle 编辑器上)。
- 单击“试用我”(预览时)。
- 单击运行移动到另一个页面,变量将被重新设置。
Code :
代码 :
window.currentChild = false;
$("#tryme").click(function() {
if (currentChild) currentChild.close();
const child = window.open("about:blank", "lmao", 'width=250,height=300');
currentChild = child;
//Scrope script in child windows
child.frames.eval(`
setInterval(function () {
if (!window.opener.currentChild)
window.opener.currentChild = window;
}, 500);
`);
});
setInterval(function() {
console.log(currentChild)
if (!currentChild || (currentChild && currentChild.closed))
$("p").text("No popup/child. :(")
else
$("p").text("Child detected !")
}, 500);