5

I want to create a button on my website that would close the current browser tab. Searching on stack overflow I got a solution but it isn't working.

This JavaScript code is:

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

HTML code is:

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

So, How can I do this using jQuery or JavaScript for all browser?

codeKitShagor
  • 81
  • 1
  • 8

1 Answers1

0

windows.close will work when you open the window..

Some 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 when you open, you also need to tell let Js/jQ know what to close:

somethingThatYouDidOpen.close();
  • Actually I need to close my current window without creating new window using `window.open()`. Is it possible? – codeKitShagor Feb 02 '15 at 11:43
  • No, it's not. You can create a overlayer or modal to show content and close it or direct to another page.. but browser denied to close when not did open by script. –  Feb 02 '15 at 14:52