-1

Ok, I know that you cant close a window using JS unless your JS script also opened that window. So, is it possible to open a new window in place of the current window & then close it?

I have a button that onclick send some value to my DB once they hit ok on the popup, the window should close.

Any specific way this can be done?

Miah Thompson
  • 239
  • 2
  • 12
  • Just don't use a popup window, many browsers block those by default and also the browser might choose how it's opened (it may also open a new tab instead of a real window). – xander Nov 29 '17 at 13:08
  • so how do I get that behaviour of sending the user entry, have them press ok on an alert and then the page/tab close? or is that not doable? – Miah Thompson Nov 29 '17 at 13:09
  • 1
    Show modal dialog (popular frameworks offer these) and populate it how you want to. The user interacts (changes things then presses a button) and you use Ajax to post the data to your server, then close the modal dialog - still in the same browser and on the original page. – Reinstate Monica Cellio Nov 29 '17 at 13:10
  • Possible duplicate of [Open URL in new window with Javascript](https://stackoverflow.com/questions/14132122/open-url-in-new-window-with-javascript) – Marcel Nov 29 '17 at 13:15
  • I can open the window fine, it's just not closing. That question is about how to open, mine is more how to close. – Miah Thompson Nov 29 '17 at 13:58
  • Maybe this [How can I close a browser window ...](https://stackoverflow.com/questions/57854/how-can-i-close-a-browser-window-without-receiving-the-do-you-want-to-close-thi) might help – Sorix Nov 29 '17 at 14:12
  • @Sorix That question is about closing a window without the popup you get when you weren't the opener. It's not relevant here as the OP is opening the window from their own script. – Reinstate Monica Cellio Nov 29 '17 at 14:19
  • @Archer window.close() should work in this case. I agree that it is not the same question but if the answer is the same, maybe there is no need to open a new question for every similar problem. On the other hand, if window.close() is not working, the question might need further detailing. – Sorix Nov 29 '17 at 14:28
  • @Sorix yes, `window.close()` is exactly what the OP needs, but the question you linked covers a different topic. It's misleading if all that's needed is closing a window, and will just confuse the issue. – Reinstate Monica Cellio Nov 29 '17 at 14:34
  • @marcel can you not downvote this? It's not a duplicate. – Miah Thompson Nov 29 '17 at 15:11
  • I’m not the one who downvoted – Marcel Nov 29 '17 at 15:17

1 Answers1

0

As you say, you can close a window if it was opened by a script. If your 2nd window has a button in it like this...

<button id="closeWindow">click me to close this window</button>

Then you can attach this Javascript...

document.querySelector("#closeWindow").addEventListener("click", function() {
    window.close();
});

Add that script into the page that you show in the new window and it will close it when you click the button.

However, as I said in my comment above, I'd really recommend using a modal dialog instead, like you see with the likes of Bootstrap etc.. They're much nicer, very user friendly and easier to work with.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67