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();
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.
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.
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:
Definition and Usage. The close() method closes the current window. Tip: This method is often used together with the open() method.
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','');"
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.
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.
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
window.open()
)window.open("", "_self");
window.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With