0

When the user create an event.in this case it is click event, the current browser tab should be closed. window.close() only works if the browser tab is opened with js i think. I got some stackoverflow queries and answers during searching. Unfortunately I could not get a solution and apparently stuck with it. Maybe i am missing something . I am looking forward for the solution as soon as possible.

Sandy B
  • 113
  • 1
  • 15

1 Answers1

0

Came across this requirement. This link helped me:

How to close current tab in a browser window?

You can use:

    window.close();

    //or you can define a function yourself, like below
    function customClose(){
      if(confirm("Are you sure?") {
        window.close();
      }
    }

Note: Please check its compatibility with IE6 (not sure about this version).

  • As you can see from the thread you listed above, the tab get closed if the tab is previously opened with js. I tried the given method on that tread which uses close() function after an alert using js. There is no response after confirm the alert box to close the window. Currently using chrome 68 for testing purpose – Sandy B Aug 07 '18 at 06:08
  • Correct me if I understood you wrongly, You mean that you used alert("Are you sure?") instead of confirm("Are you sure?") ?? If that is the case, chrome's alert always returns undefined, whenever you click the "Ok" button in the alert box. I would suggest you to use confirm instead of alert. – Anmol Deora Aug 07 '18 at 06:22
  • All right. I've used confirm and tested it. Tab did not close. The confirm box did not do anything after the confirmation. It just disappears without doing anything.. – Sandy B Aug 07 '18 at 06:40
  • Oh, interesting. You close the confirm box by clicking on Ok or Cancel ? You need to click Ok. Also, can you simply open a new tab, open the chrome developer console and enter : confirm("test this"). The moment you will press enter, a confirm dialog box will open. If you click on Ok, it should print true. Else, false. Can you confirm this much? – Anmol Deora Aug 07 '18 at 09:22
  • Of course i can confirm that . For your first query , i clicked Ok. (because i wanted the tab to be closed) .The console error was "Scripts may close only the windows that were opened by it" as we know. To make everything clear i opened a new tab and tested , and it returns true . – Sandy B Aug 07 '18 at 10:11
  • Oh, so this is the error. Mate, can you tell me how, exactly are you trying to open the page? Do you use any frameworks (vue/angular etc) for routing and opening the pages OR is there anything else? – Anmol Deora Aug 07 '18 at 10:46
  • For this page i am using jquery library and geoplugin script . Then my own js scripts and css files. Its the home page.I want this tab to be closed from an onclick event . – Sandy B Aug 08 '18 at 04:53