1

I want to close the current tab using javascript. But i did not got any solutions. I searched the whole internet for this and some of the post suggest below solution.

 $("#click").click("click", function(element) {
     window.top.close();
 });

But after clicking the button getting Scripts may close only the windows that were opened by it. as a warning in the console. Is there any possible way to close the current tab which are not opened by any script?

dhamo dharan
  • 712
  • 1
  • 10
  • 25
  • 2
    Assuming you're running the JS logic *within* the opened tab, then `window.close()` will work. However, as the error states, you cannot close another tab which was not opened through code. – Rory McCrossan Aug 02 '19 at 16:28
  • Use `$(selector).on('click', function(e))` rather than `$("#click").click("click", function(e)` – Sonson Ixon Aug 02 '19 at 16:30
  • i am using jQuery 1.3. I think $(selector).on('click', function(e)) was Introduced in jQuery 2.3 – dhamo dharan Aug 03 '19 at 07:55

1 Answers1

3

You cannot.

https://developer.mozilla.org/en-US/docs/Web/API/Window/close

This method can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.

Rafael Duarte
  • 569
  • 6
  • 21