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?

3
  • 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. Commented Aug 2, 2019 at 16:28
  • Use $(selector).on('click', function(e)) rather than $("#click").click("click", function(e) Commented Aug 2, 2019 at 16:30
  • i am using jQuery 1.3. I think $(selector).on('click', function(e)) was Introduced in jQuery 2.3 Commented Aug 3, 2019 at 7:55

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

got the clarity, thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.