1

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?

j08691
  • 204,283
  • 31
  • 260
  • 272
Andre
  • 11
  • 1
  • 3
  • potential duplicate: https://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome – TCooper Jan 28 '21 at 01:15
  • 1
    Would it be possible to edit you code so that it's in a runnable snippet that the community could help diagnose? – karolus Jan 28 '21 at 04:00
  • Does a button `onclick="window.close()" handler work (the code seems overly complicated)? 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 Jan 28 '21 at 09:10

1 Answers1

2

According 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.org/en-US/docs/Web/API/Window/close#closing_the_current_window

pncln
  • 70
  • 5
  • 1
    I'd add it's rarely ever good UX to close the window for the user. There's a reason large sites handling confidential information give alerts telling you it's best to close your window/browser to prevent stored sessions instead of trying to do it for you. It's an unnatural/unpleasant experience as a user in 9/10 instances. – TCooper Jan 28 '21 at 01:14
  • Thanks for your quick reply. So if a user clicks a link (or button) on a php generated web page and that href loads a new php program into another browser tab using target="_blank", how does the user Close that window / tab? Do they press 'the X' on the tab? I was using a link to window.close(), because it seemed more elegant..... Is there another way of doing this? – Andre Jan 28 '21 at 02:23
  • You can do that (if I understood you correctly, tell me if not :) ). Here's how: https://pastebin.pl/view/34065e2e – pncln Jan 28 '21 at 02:31
  • It looks like your 'parent' page opens a child window that gets loaded with www.google.com. You then close the child window from the parent. I want to close the child window by the application that gets loaded (in your example: Google). – Andre Jan 28 '21 at 02:52
  • 1
    I think I found a solution. I've changed the 'parent' program so that instead of simply using an href with the targer = "_blank", I pass the URL into a javascript function that performs a window.Open. This way both the window Open and Close are controlled by Java Script. So far, so good. Thanks for everybody's help! – Andre Jan 28 '21 at 16:42