5

I have a logout button on which if user clicks, browser tab need to be closed. I have tried with the following method:

window.close(); 

but its not working in any of the browsers.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dvln
  • 111
  • 5
  • 23
  • I don't think, this would be possible. If you really want to close tab ( window ). Open your website in a separate window with `window.open` – Jashwant Jan 30 '14 at 05:24
  • check these links,http://stackoverflow.com/questions/28166936/close-aspx-using-javascript-jquery/28167044#28167044 – Abdul Jan 27 '15 at 12:05

1 Answers1

2

If you try to close any window with window.close() you will get error message like

"Scripts may not close windows that were not opened by script" (Message Depends on Browser)

so, its mean you must have to open windows by JavaScript first to close by your code, like:

window.open("http://www.google.com/");

now you can easily close this window by window.close().

.

Another advantage using this functionality is you can also close Child Window from

Parent Windows`. like:

var win;
$('#click').click(function() {
    win = window.open("test3.html", "something", "width=550,height=170");
});

function closeit() {
    win.close();
}
124
  • 2,757
  • 26
  • 37