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();
This question is related to
javascript
html
TRY this out it works fine ...
<!DOCTYPE html>
<html>
<body>
<input type="button" onclick="openWin()" value="open"/>
<input type="button" onclick="closeWin()" value="close"/>
<script>
var myWindow;
function openWin()
{
myWindow = window.open("","myWindow","width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
}
function closeWin()
{
myWindow.close();
}
</script>
</body>
</html>
this works as you want it to work
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:
onClick="javascript:window.close('','_parent','');", now in 2019.
I suggest to put id
to the input, and close the window by callback function as the following:
<input id="close_window" type="button" class="btn btn-success"
style="font-weight: bold;display: inline;"
value="Close">
The callback function as the following:
<script>
$('#close_window').on('click', function(){
window.opener = self;
window.close();
});
</script>
I think sometimes onclick
doesn't work, check the following answer also.
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();
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 can try
function closed(){_x000D_
_x000D_
setTimeout("window.close()", 500);_x000D_
}
_x000D_
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. Then close it using window.close(). The below script should go into the parent window, not the child window. You could run this after running the script to open the child.
<script type="text/javascript">
window.open('','_parent','');
window.close();
</script>
Works only in Google Chrome with self.close();
. Tested in v48.
window.close()
won't do what you want as the documentation for it clearly states that scripts may close only the windows that were opened by it.
If you can't close windows that aren't opened by the script, then you can destroy your page using this code:
document.getElementsByTagName ('html') [0] .remove ();
I found this method by Jeff Clayton. It is working for me with Firefox 56 and Chrome (on Windows and Android).
Did not try all possibilities. On my side, I open a window with a target name in my A links, for example, target="mywindowname", so all my links open inside the same window name: mywindowname.
To open a new window:
<a href="example-link.html" target="mywindowname">New Window</a>
And then in the new window (inside example-link.html):
<a href="#" onclick="return quitBox('quit');"></a>
Javascript:
function quitBox(cmd) {
if (cmd=='quit') {
open(location, '_self').close();
}
return false;
}
Hope this can help anyone else that is looking for a method to close a window. I tried a lot of other methods and this one is the best I found.
Should be
<input type="button" class="btn btn-success"
style="font-weight: bold;display: inline;"
value="Close"
onclick="closeMe()">
<script>
function closeMe()
{
window.opener = self;
window.close();
}
</script>
Source: Stackoverflow.com