3

It is not a duplicate question please Dear All, I am new to Jquery, Please help me how to close the Current opened Tab in a application using Jquery on click of YES/Confirm button in a model window as shown in a image. I have tried below function, its not closing current tab instead it closed all the tabs. Please help me Image

window.open('', '_self').close();
         window.top.close();
         window.close();
Barmar
  • 741,623
  • 53
  • 500
  • 612
Shankar
  • 71
  • 1
  • 7
  • JavaScript can't manipulate tabs. – Barmar Mar 14 '19 at 00:58
  • Since i am opening the window/tab not using window.open so. I am able to close the particular tab by TabID using programmatically – Shankar Mar 14 '19 at 15:54
  • JS can only close a window/tab that it opened. If you don't use `window.open`, you can't close it. – Barmar Mar 14 '19 at 15:56
  • **I am able to close the particular tab by TabID using programmatically** You keep stating that, but it's not true. Why do you think you're able to do that? Where do you get the TabID from? – Barmar Mar 14 '19 at 15:58
  • I think a browser extension can do this, but not JavaScript in an ordinary page. – Barmar Mar 14 '19 at 15:58

2 Answers2

2

before you close the current tab, you need to open it using "window.open()". the window.close button works only the windows that opened by it.

mackbex
  • 69
  • 4
  • Thanks for your response, as i have mentioned I am not opening the window using windo.open, so could you please help me, how I can close the parent window from the modal window as shown in the image – Shankar Mar 14 '19 at 01:15
  • 1
    Since i am opening the window/tab not using window.open so. I am able to close the particular tab by TabID using programmatically – Shankar Mar 14 '19 at 15:54
0

You can close the current tab with this javascript line:

window.top.close();

But there are things that you should keep in mind:

  • This will only work if the tab is also opened programmatically.

    By using window.open();, you can close the opened tab with the javascript line above.

  • Not all browsers support this solution

You can also visit this link for reference. I hope this helps.

Florenz
  • 53
  • 6
  • Thanks for your response, as i have mentioned I am not opening the window using windo.open, so could you please help me, how I can close the parent window from the modal window as shown in the image – Shankar Mar 14 '19 at 01:15
  • Hi @Shankar. Well on that case, you cannot close the tab. Javascript can't manipulate tabs that are not opened programmatically. – Florenz Mar 14 '19 at 01:19
  • Thanks again, but there has to be way to identify the tab name and close right ? in my case, I have the tab name , – Shankar Mar 14 '19 at 01:22
  • @Shankar You still can't. You'll only get this notification when trying to close the specified tab `"Scripts may not close windows that were not opened by script"` – Florenz Mar 14 '19 at 01:27
  • `window.top` is the top window that contains the current iframe. It can't be used to refer to a completely different tab in the browser. – Barmar Mar 14 '19 at 15:57