Home »
JavaScript Examples
JavaScript - Close Current Tab on Button Click with Confirmation
In this code snippet we will learn how to close current tab on button click event with confirmation box. Here we will implement JavaScript function to close current tab with confirmation box.
Close Current Tab with Confirmation Box
JavaScript function:
<script type="text/javascript">
function closeCurrentTab() {
var conf = confirm("Are you sure, you want to close this tab?");
if (conf == true) {
close();
}
}
</script>
JavaScript and HTML Code to Close Current Tab on Button Click with Confirmation
<!--JavaScript - Close Current Tab on Button Click with Confirmation.-->
<html>
<head>
<title>JavaScript - Close Current Tab on Button Click with Confirmation.</title>
<script type="text/javascript">
function closeCurrentTab() {
var conf = confirm("Are you sure, you want to close this tab?");
if (conf == true) {
close();
}
}
</script>
</head>
<body style="text-align: center;">
<h1>JavaScript - Close Current Tab on Button Click with Confirmation.</h1>
<p><input type="button" value="Close Tab." onclick="closeCurrentTab()"></p>
</body>
</html>
Result
JavaScript Examples »