How to close current browser tab by clicking a button?

I want to create a button on my website that closes the current browser tab. Search stack overflow I have a solution, but it does not work.

This JavaScript Code:

function close_window(){ if (confirm("Are you sure to close window?")) { close(); } } 

HTML code:

 <button onclick="close_window();"> Close! </button> 

So how can I do this using jQuery or JavaScript for all browsers?

+5
source share
1 answer

windows.close will work when you open the window.

Example:

 var tab; $('#click').click(function() { tab = window.open("blackie.html", "something", "width=650,height=50"); }); $('#close').click(function() { tab.close(); }); 

.close () will only work on opening, you also need to report that Js / jQ knows what to close:

 somethingThatYouDidOpen.close(); 
0
source

Source: https://habr.com/ru/post/1212432/


All Articles