is there any way to close a browser tab with javascript? [duplicate]

2

I have a script that only works locally. Is there any way to close the browser tab using javascript ? If so, could you give examples?

    
asked by anonymous 16.07.2015 / 20:10

3 answers

4

Use window.close() . It closes the current tab / window.

HTML:

<input type="button" onclick="window.close()" value="Fechar janela" />

<button onclick="window.close()">Fechar janela</button>

<a href="javascript:void()" onclick="window.close()">Fechar janela</a>

Pure Javascript:

document.getElementById("#botaoOuLink").onclick = function()
{
    window.close();
}

With jQuery:

$(elemento).on("click", function() 
{
    window.close();
});
    
16.07.2015 / 20:13
2

I encountered the same problem and solved using:

window.frames.closewindow();

This is because I work with a frame-based system, so I did not test with another type of system. Worth a try.

    
11.04.2016 / 23:53
1

Javascript function for closing:

If you want the user to confirm the page output:

function close_window() {
  if (confirm("Fechar Janela?")) {
    close();
  }
}

If not:

function close_window() {
    close();
}

With HTML:

<a href="javascript:close_window();">Fechar Janela</a>

or

<a href="#" onclick="close_window();return false;">Fechar Janela</a>

Original answer: SOen

    
16.07.2015 / 20:13