Close popup window

戏子无情 提交于 2019-12-18 03:59:14

问题


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


回答1:


An old tip...

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



回答2:


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

window.open

then

window.close

will work. Or else not.




回答3:


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:

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



回答4:


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)

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



回答5:


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...

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

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 !




回答6:


try this

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


来源:https://stackoverflow.com/questions/14125648/close-popup-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!