Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript close current window

please help java script about close current window. this is my code and it does not work.

<input type="button" class="btn btn-success"
                     style="font-weight: bold; display: inline;"
                     value="Close"
                     onclick="closeMe()">
function closeMe()
{
    window.opener = self;
    window.close();
}

I tried this but it doesn't work either:

var win = window.open("", "_self");
win.close();
like image 750
Kuc Ku Avatar asked Apr 26 '14 05:04

Kuc Ku


People also ask

How to close a window in JavaScript?

In JavaScript, we can close a window only if it is opened by using window.open method: But to close a window which has not been opened using window.open (), you must To close your current window using JS, do this. First open the current window to trick your current tab into thinking it has been opened by a script.

How to close the current tab in JavaScript?

Below is the method that closes the current window tab: Before going to discuss further, I think you should aware of the fact that window.close () method will able to close only the tab that is open by using the window.open () method. So, to close the current tab in javaScript, it must have to be opened by using the window.open () method.

How do I close the current window?

The close () method closes the current window. Tip: This method is often used together with the open () method. Open "www.w3schools.com" in a new window, and use close () to close the window:

What is the use of close() method in JavaScript?

Definition and Usage. The close() method closes the current window. Tip: This method is often used together with the open() method.


4 Answers

I know this is an old post, with a lot of changes since 2017, but I have found that I can close my current tab/window with the following now in 2019:

onClick="javascript:window.close('','_parent','');"
like image 193
Errol Jacoby Avatar answered Oct 16 '22 13:10

Errol Jacoby


** Update **

Since posting the answer the latest versions of browsers prevent the command from working. I'll leave the answer visible but note it ONLY works on older browser versions.


Most browsers prevent you from closing windows with javascript that were not opened with window.open("https://example_url.com") however, it is still possible to close the current window using the following command:

window.open('','_self').close()

This loads a blank url (the first argument) in the current window (the second argument) and then instantaneously closes the window. This works because when close() is called, the current window has been opened by javascript.

like image 33
Fowler Avatar answered Oct 16 '22 13:10

Fowler


You cannot close a window with javascript that you did not open. It will not work in chrome or firefox.

See https://stackoverflow.com/a/19768082/2623781 for more information.

like image 8
nate.merlin Avatar answered Oct 16 '22 14:10

nate.merlin


In JavaScript, we can close a window only if it is opened by using window.open method:

window.open('https://www.google.com');
window.close();

But to close a window which has not been opened using window.open(), you must

  1. Open any other URL or a blank page in the same tab
  2. Close this newly opened tab (this will work because it was opened by window.open())
window.open("", "_self");
window.close();
like image 8
Neha Soni Avatar answered Oct 16 '22 13:10

Neha Soni