close window javascript code example
Example 1: javascript close window
<!DOCTYPE html>
<html>
<body>
<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>
<script>
var myWindow;
function openWin() {
myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
}
function closeWin() {
myWindow.close();
}
</script>
</body>
</html>
Example 2: html close window button
<button type="button" onclick="javascript:window.close()"> Name </button>
Example 3: how to close tab by javascript
// IF you use in a button
<button onclick="window.close();">Exit</button>
// if you use in function
function closeWin() {
myWindow.close(); // Closes the new window
}
// But it will only work when you open a window through your code like this
function openWin() {
myWindow = window.open("", "myWindow", "width=200, height=100"); // Opens a new window
}
// You can't close the current window that was open by browser. beacuse browser don't give permission for this