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?
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?
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();
});
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.
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