Javascript 关闭弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14125648/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:47:09  来源:igfitidea点击:

Close popup window

javascriptpopup

提问by Drew

I have a popup window which is opened using this code:

我有一个使用以下代码打开的弹出窗口:

function _openpageview(fieldid,objectid,opennew)
{
var url='/s_viewpagefield.jsp?fieldid='+fieldid+'&codedid='+objectid;

  web_window = window.open(url,'_blank', 'menubar=yes,location=no,scrollbars=yes,width=800,height=600,status=no,resizable=yes,top=0,left=0,dependent=yes,alwaysRaised=yes');
   web_window.opener = window;
   web_window.focus();


}

How can I close that popup from within the popup?

如何从弹出窗口中关闭该弹出窗口?

window.close();
self.close();
web_window.close();

all did not work

都没有工作

回答by molokoloco

An old tip...

一个老提示...

var daddy = window.self;
daddy.opener = window.self;
daddy.close();

回答by koma

You can only close a window using javascript that was opened using javascript, i.e. when the window was opened using :

您只能使用使用 javascript 打开的 javascript 关闭窗口,即当窗口使用打开时:

window.open

then

然后

window.close

will work. Or else not.

将工作。否则不会。

回答by ic3b3rg

Your web_window variable must have gone out of scope when you tried to close the window. Add this line into your _openpageview function to test:

当您尝试关闭窗口时,您的 web_window 变量必须超出范围。将此行添加到您的 _openpageview 函数中以进行测试:

setTimeout(function(){web_window.close();},1000);

回答by Kevin Fisher

For such a seemingly simple thing this can be a royal pain in the butt! I found a solution that works beautifully (class="video-close" is obviously particular to this button and optional)

对于这样一件看似简单的事情,这可能是一件令人头疼的事情!我找到了一个效果很好的解决方案(class="video-close" 显然是这个按钮所特有的,并且是可选的)

 <a href="javascript:window.open('','_self').close();" class="video-close">Close this window</a>

回答by Balmipour

In my case, I just needed to close my pop-up and redirect the user to his profile page when he clicks "ok" after reading some message I tried with a few hacks, including setTimeout + self.close(), but with IE, this was closing the whole tab...

在我的情况下,我只需要关闭我的弹出窗口并将用户重定向到他的个人资料页面,当他阅读了一些我尝试使用一些技巧的消息后单击“确定”,包括 setTimeout + self.close(),但使用 IE ,这是关闭整个标签...

Solution : I replaced my link with a simple submit button.
<button type="submit" onclick="window.location.href='profile.html';">buttonText</button>. Nothing more.

解决方案:我用一个简单的提交按钮替换了我的链接。
<button type="submit" onclick="window.location.href='profile.html';">buttonText</button>. 而已。

This may sound stupid, but I didn't think to such a simple solution, since my pop-up did not have any form.

这听起来可能很愚蠢,但我没想到这么简单的解决方案,因为我的弹出窗口没有任何形式。

I hope it will help some front-end noobs like me !

我希望它能帮助一些像我这样的前端菜鸟!

回答by Srinivas

try this

尝试这个

  if(false == web_window.closed)
  {
     web_window.close ();
  }