My application opens new PHP from an icon or link in a new browser tab. For several months now, I've been using a simple JavaScript function to close the current browser tab.
Suddenly, over the past few days, the Close link only works when the Window is first opened. The moment you do something on the page (click a link, press a submit button), it doesn't work. I'm at a loss as to what the problem can be. Nothing has changed in the code.
The HTML href statement is:
<a href="javascript:windowClose()"><span>Close</span></a>
And the JavaScript function is:
function windowClose() {
window.open('','_parent','');
window.close();
}
This is happening with both Edge and Chrome on a Windows 10 platform.
Could it be caused by some update to the O/S, Chrome or Edge application?
My application opens new PHP from an icon or link in a new browser tab. For several months now, I've been using a simple JavaScript function to close the current browser tab.
Suddenly, over the past few days, the Close link only works when the Window is first opened. The moment you do something on the page (click a link, press a submit button), it doesn't work. I'm at a loss as to what the problem can be. Nothing has changed in the code.
The HTML href statement is:
<a href="javascript:windowClose()"><span>Close</span></a>
And the JavaScript function is:
function windowClose() {
window.open('','_parent','');
window.close();
}
This is happening with both Edge and Chrome on a Windows 10 platform.
Could it be caused by some update to the O/S, Chrome or Edge application?
Share Improve this question edited Jan 28, 2021 at 1:03 j08691 208k32 gold badges269 silver badges280 bronze badges asked Jan 28, 2021 at 0:35 AndreAndre 111 gold badge1 silver badge3 bronze badges 3- potential duplicate: stackoverflow./questions/19761241/… – TCooper Commented Jan 28, 2021 at 1:15
- 1 Would it be possible to edit you code so that it's in a runnable snippet that the munity could help diagnose? – karolus Commented Jan 28, 2021 at 4:00
- Does a button `onclick="window.close()" handler work (the code seems overly plicated)? Also, does "taking a link" refer to an intra-page or off-page link, and what is the use case of closing a page during form submission? – traktor Commented Jan 28, 2021 at 9:10
1 Answer
Reset to default 2According to MDN, "scripts can not close windows, they had not opened"
If the tab has been opened by you (you typed the url and hit enter manually), there's no way you can close the window with Javascript. However, if for instance, you started your project with npm start (React in my case), the page can be closed with the code you've provided. Though when you try to re-do it by manually opening the webpage and closing the tab - you will fail.
All in all, don't try to close the window that was not opened by js.
Reference: https://developer.mozilla/en-US/docs/Web/API/Window/close#closing_the_current_window