'Close popup window
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
Solution 1:[1]
An old tip...
var daddy = window.self;
daddy.opener = window.self;
daddy.close();
Solution 2:[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.
Solution 3:[3]
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>
Solution 4:[4]
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);
Solution 5:[5]
in my case (joomla opened popup) I had to use this:
onclick="submit(); window.parent.location.reload(false);window.parent.SqueezeBox.close();"
Hope this may help
Solution 6:[6]
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 !
Solution 7:[7]
try this solution
<html>
<script>
var newWindow;
function openWindow() {
newWindow = window.open("", "myWindow", "width=200,height=100");
newWindow.document.write('<a href="javascript:window.close();" class="video-close">Close this window</a>');
}
function closeWindow() {
newWindow.close();
}
</script>
<h3 style="color:brown"> Close Window Example </h3>
<body>
<button onclick="openWindow()">Open New Window</button>
<br><br>
<button onclick="closeWindow()">Close New Window </button>
</body>
</html>
Solution 8:[8]
Close a popup after print.
In the parent windows
let IframeLink = '../Sale/PriceKOT?SalesID=@(Model.SalesInfo.SalesID)&PrintTypeID=3';
var newwindow = window.open(IframeLink, "KOT Print", 'width=560,height=550,toolbar=0,menubar=0,location=0');
if (window.focus) { newwindow.focus() }
function closeThisPopUp() {
newwindow.close();
printf();
}
In the child or popup window
window.onafterprint = function (event) {
opener.closeThisPopUp();
window.close;
};
Solution 9:[9]
using window.close()
javascript:window.close()
Try This:
<html>
<header>
<script>
let newWebWindow;
function openPageView() {
newWebWindow = window.open("", "window", "width=200,height=100");
newWebWindow.document.write(`<a href="javascript:window.close();"class="video-close">Close this window</a>`);
}
function closePageView() {
newWebWindow.close();
}
</script>
</header>
<body>
<h3 style="color:brown"> Close Window Example </h3>
<button onclick="openPageView()">Open Page</button>
<button onclick="closePageView()">Close Page</button>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow