Javascript 在使用Jquery打开弹出窗口之前,检查它是否已经打开
在打开弹出窗口之前,我想检查弹出窗口是否已经打开。 我如何使用Jquery完成它 下面是我打开新弹出窗口的代码:Javascript 在使用Jquery打开弹出窗口之前,检查它是否已经打开,javascript,jquery,html,Javascript,Jquery,Html,在打开弹出窗口之前,我想检查弹出窗口是否已经打开。 我如何使用Jquery完成它 下面是我打开新弹出窗口的代码: window.open("mystopchat.php?stat=1&session="+data['myid1']['session_id'][i],"win1","width=500,height=500"); 现在,在调用此函数之前,我希望确保此弹出窗口尚未打开。var newWindow=null; 函数openwindow() { //仅当newWindow为空
window.open("mystopchat.php?stat=1&session="+data['myid1']['session_id'][i],"win1","width=500,height=500");
现在,在调用此函数之前,我希望确保此弹出窗口尚未打开。var newWindow=null;
函数openwindow()
{
//仅当newWindow为空(尚未打开)时才打开新窗口
//或者如果它关闭了
如果((newWindow==null)| |(newWindow.closed))
newWindow=window.open(…);
}
试试这个(你会知道是否调用了打开的窗口):
这是我使用的一个小技巧,也许你可以使用它:
var winRef; //This holds the reference to your page, to see later it is open or not
function openWindow() {
var url = //Your URL;
if (typeof (winRef) == 'undefined' || winRef.closed) {
//create new, since none is open
winRef = window.open(url, "_blank");
}
else {
try {
winRef.document; //if this throws an exception then we have no access to the child window - probably domain change so we open a new window
}
catch (e) {
winRef = window.open(url, "_blank");
}
//IE doesn't allow focus, so I close it and open a new one
if (navigator.appName == 'Microsoft Internet Explorer') {
winRef.close();
winRef = window.open(url, "_blank");
}
else {
//give it focus for a better user experience
winRef.focus();
}
}
}
希望有帮助。以下是我的建议:
function authorize(callback) {
if(!document.authorize) {
console.log('Opening authorization window...');
document.authorize = window.open('popup.html','tw','width=300,height=200');
}
if(document.authorize.closed) {
console.log('Authorization window was closed...');
setTimeout(callback,0);
} else {
setTimeout(function(){
console.log('Authorization window still open...');
authorize(callback);
},1000);
}
return false;
}
function test() {
authorize(function(){
alert('teste');
});
}
请看[这篇文章][1],这可能会有所帮助。[1] :将一个变量ispopup设为变量并将其设置为“false”。打开窗口后,将其设置为true,如if(ispoup!=true){window.open()}其他{//somothing its open;}是的,这对2014年的某人很有帮助!:)@ChankeyPathak很乐意帮忙:)
var winRef; //This holds the reference to your page, to see later it is open or not
function openWindow() {
var url = //Your URL;
if (typeof (winRef) == 'undefined' || winRef.closed) {
//create new, since none is open
winRef = window.open(url, "_blank");
}
else {
try {
winRef.document; //if this throws an exception then we have no access to the child window - probably domain change so we open a new window
}
catch (e) {
winRef = window.open(url, "_blank");
}
//IE doesn't allow focus, so I close it and open a new one
if (navigator.appName == 'Microsoft Internet Explorer') {
winRef.close();
winRef = window.open(url, "_blank");
}
else {
//give it focus for a better user experience
winRef.focus();
}
}
}
function authorize(callback) {
if(!document.authorize) {
console.log('Opening authorization window...');
document.authorize = window.open('popup.html','tw','width=300,height=200');
}
if(document.authorize.closed) {
console.log('Authorization window was closed...');
setTimeout(callback,0);
} else {
setTimeout(function(){
console.log('Authorization window still open...');
authorize(callback);
},1000);
}
return false;
}
function test() {
authorize(function(){
alert('teste');
});
}