Closing the Current Browser Tab with User Confirmation
Closing the current browser tab without affecting other tabs can be achieved using JavaScript's window.close() method.
To create a link that prompts the user for confirmation before closing the tab, the following steps can be followed:
function closeTab() { if (confirm("Close Tab?")) { window.close(); // Close the current tab } }
<a href="javascript:closeTab();">Close Tab</a>
When the user clicks on the link, the closeTab() function will execute, displaying the confirmation dialog with Yes and No buttons. If the user clicks Yes, the current tab will be closed. If they click No, nothing will happen.
Note:
The above is the detailed content of How Can I Close a Browser Tab with User Confirmation Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!